Solved

JavaScript: innerHTML removes closing slash!

Posted on 2011-02-22
7
793 Views
Last Modified: 2012-08-13
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
Comment
Question by:hankknight
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34954276
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
 
LVL 42

Assisted Solution

by:David S.
David S. earned 150 total points
ID: 34954432
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
 
LVL 16

Author Comment

by:hankknight
ID: 34954699
Is there a good way to get THE ACTUAL CONTENT?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 42

Expert Comment

by:David S.
ID: 34954714
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
 
LVL 16

Author Comment

by:hankknight
ID: 34954782
Using AJAX for this seems like using a cannon to shoot a fly.
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 150 total points
ID: 34954975
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 200 total points
ID: 34960645
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Parallax 1 38
html form to write data to csv 24 96
Replace &lt; with < 14 56
Creating a dynamic URL  - link to results on Airbnb availability for tomorrow 7 26
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now