aaa<br /> gets turned into aaa<BR> with innerHTML

Y

yawnmoth

<div id="demo">aaa<br /></div>

<script>
alert(document.getElementById('demo').innerHTML);
</script>

Why, in Firefox, does that return "aaa<br>"? And why, in Opera, does
that return "aaa<BR>"? Why isn't it returning the exact HTML as
opposed to some strange sort of internally modified version?
 
M

Martin Honnen

yawnmoth said:
<div id="demo">aaa<br /></div>

<script>
alert(document.getElementById('demo').innerHTML);
</script>

Why, in Firefox, does that return "aaa<br>"? And why, in Opera, does
that return "aaa<BR>"? Why isn't it returning the exact HTML as
opposed to some strange sort of internally modified version?

Well the browser does not store the source, it builds a DOM tree with
nodes and serializes as needed when you access innerHTML.
How would you expect innerHTML to give you any result for dynamically
created elements if it simply returned the source text?
 
Y

yawnmoth

Well the browser does not store the source, it builds a DOM tree with
nodes and serializes as needed when you access innerHTML.
How would you expect innerHTML to give you any result for dynamically
created elements if it simply returned the source text?

Hmmmm - good point.
 
L

Luuk

yawnmoth schreef:
<div id="demo">aaa<br /></div>

<script>
alert(document.getElementById('demo').innerHTML);
</script>

Why, in Firefox, does that return "aaa<br>"? And why, in Opera, does
that return "aaa<BR>"? Why isn't it returning the exact HTML as
opposed to some strange sort of internally modified version?

IE8 also returns "aaa<BR>" (just in case someone wanted to know.... ;-)
 
R

RobG

Hmmmm - good point.

Also, browsers do quite a bit of error correction where necessary, the
(non-standard but widely supported) innerHTML property represents what
the browser made of the source.

Note that the innerHTML property has not been standardised, so while
it is reasonably consistent for most elements, there are various
foibles between browsers. Do not depend on it to return consistent
HTML for a document fragment, particularly in regard to attributes,
e.g.:

var div = document.createElement('div');
var inp = document.createElement('input');
inp.value = 'foo';
div.appendChild(inp);
alert(div.innerHTML);

IE shows "<INPUT value=foo>"
Firefox shows "<input>"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top