JavaScript: innerHTML removes closing slash!

Posted on 2011-02-22
Last Modified: 2012-08-13
When I use innerHTML, <br /> becomes <br>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> 
<html lang="en" xmlns=""> 

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

<script type="text/javascript">

Open in new window

Question by:hankknight
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 23

Expert Comment

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>.
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.
LVL 16

Author Comment

ID: 34954699
Is there a good way to get THE ACTUAL CONTENT?
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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.
LVL 16

Author Comment

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

Assisted Solution

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.
LVL 82

Accepted Solution

leakim971 earned 200 total points
ID: 34960645
Check this article :
Have a look at the end of this page :

So in your code you can left this tags away from your "analyse" and don't worry about the number of their occurences.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slush on text 2 29
Boolean 13 51
JavaScript character count without counting spaces in Textarea (Code provided) 3 55
Drag & Drop Error 5 34
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

710 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