0

I am trying to get the innerhtml of an html element which contains self closing br tags<br/> but using .innerhtml converts the <br/> to <br> tag. I am working with IE10 And it has no XHTML parser to get innerhtml. This is the html:

<div>
<span>line 1<br/>line 2<br/>line 3</span>
</div>

I want to retain the self closing tag as it is but want to avoid using regEx. Can anyone help me with some other approach to achieve this?

1 Answers1

1

innerHTML converts it to <br> because it should be <br> in html. If you want to use it as <br /> in XHTML, you could replace <br> with <br /> after getting the inner html. You could refer to the sample below:

var a = document.getElementsByTagName("div")[0];
var b = a.innerHTML.split("<br>").join("<br />");
console.log(b);
<div>
  <span>line 1<br />line 2<br />line 3</span>
</div>
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22