Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 935
  • Last Modified:

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

0
hankknight
Asked:
hankknight
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
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.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
 
hankknightAuthor Commented:
Is there a good way to get THE ACTUAL CONTENT?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
wdosanjosCommented:
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
 
leakim971PluritechnicianCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now