tags:

views:

50

answers:

3

Hi,

I have a problem with Internet Explorer browser while reading the contents of the DIV element using jQuery('DIVID').html() method. I insert a simple XML string to the DIV element. But when I try to read the content back using jQuery, some of the data is missing. It works fine in Firefox.

My code looks as shown below.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
</head>
<body>
  <p><div id="AJAXTESTDATA"></div>jQuery Simple Test.</p>
<script>
      var testAjax = document.getElementById("AJAXTESTDATA");
  var tes = '<SPAN id="tt"><DATA1><DATA2><DATA3><SPAN id="doc">TEST</SPAN></DATA3></DATA2></DATA1></SPAN>';
  alert("Actual Data:"+tes);
  testAjax.innerHTML = tes;
  var cbkData = jQuery('#AJAXTESTDATA').html();
  alert("Data after inserting to DIV:"+cbkData);
</script>
</body>
</html>

Edit: The second alert message will display the contents of the DIV tag as below: Data after inserting to DIV: <SPAN id=test><SPAN id=doccase>TEST</SPAN></DATA3></DATA2></DATA1></SPAN> So you can see that most of the start tags are missing! So whatever tag names I use, the same is the result in IE.

Could anyone let me know the reason behind the truncation of data while reading via IE?

Kind Regards, Krishna.

A: 

Could anyone let me know the reason behind the truncation of data while reading via IE?

There is no c tag in HTML 4. I assume IE is removing invalid tags in a more strict fashion than the other browsers.

If you're looking to insert the XML as text data (with all the tags visible), use .text() instead of .html().

Pekka
C tag was an example I had used. I had updated the original question with different tag names, still the same problem in IE.
@user `DATA3` etc. are not valid HTML tags either.
Pekka
A: 

I agree with @Pekka IE probably be removing invalid tags. You can add &gt; instead of > and &lt; instead of <

Diego
Yes, isn't that what I wrote?
Diego
A: 

I agree with Pekka: if you use the .text() instead of .html() then your tags will not be stripped out, though I think that you will need to use .text() to set the value instead of .innerHTML. Provided that you aren't actually expecting to display the XML data (which would not display in a sensible manner anyway) and that the AJAXTESTDATA div is actually hidden, then this will work.

jsFiddle example here

Steve Greatrex