JavaScript: innerHTML removes closing slash!

When I use innerHTML, <br /> becomes <br>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Demo</title> 
</head> 

<div id="v">
 <p>The / in these <br/> tags does not show when innerHTML is used. <br /> The slash vanishes!</p>
</div> 

<script type="text/javascript">
alert(document.getElementById('v').innerHTML);
</script>  
 
</body>  
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
Check this article : http://www.cs.tut.fi/~jkorpela/html/empty.html
Have a look at the end of this page : http://www.elharo.com/blog/software-development/web-development/2007/01/29/all-empty-tags-in-html/

So in your code you can left this tags away from your "analyse" and don't worry about the number of their occurences.
0
 
wdosanjosCommented:
I think that happens because 'innerHTML' provides the HTML content of the control not the XHTML content.  In HTML, the BR tag is only <BR>.
0
 
David S.Connect With a Mentor Commented:
That's because the browser is using an HTML parser, not an XML parser for the page. If you view the innerHTML in IE versions before 9, the tags will even be in uppercase.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
hankknightAuthor Commented:
Is there a good way to get THE ACTUAL CONTENT?
0
 
David S.Commented:
If it's a valid XHTML document, you could try using AJAX to request the page and use responseXML to find the elements you were interested in.
0
 
hankknightAuthor Commented:
Using AJAX for this seems like using a cannon to shoot a fly.
0
 
wdosanjosConnect With a Mentor Commented:
I don't think there is a way to get the original XHTML, because when you assign a value to innerHTML the browser uses it to decorate the DOM (which is not XHTML). So when you pull the information back it comes from the DOM, thus it's only HTML.
0
All Courses

From novice to tech pro — start learning today.