Getting unparsed (raw) HTML with JavaScript

html, javascript

Solution

What you have should work:

Element test:

<div id="myE">How to&nbsp;fix</div>​

JavaScript test:

alert(document.getElementById("myE​​​​​​​​").innerHTML); //alerts "How to&nbsp;fix"

You can try it out here. Make sure that wherever you're using the result isn't show `&nbsp;` as a space, which is likely the case. If you want to show it somewhere that's designed for HTML, you'll need to escape it.

Problem

I need to get the actual html code of an element in a web page. For example if the actual html code inside the element is `"How to&nbsp;fix"` Running this JavaScript: ``` getElementById('myE').innerHTML ``` Gives me `"How to fix"` which is the parsed HTML. How can I get the unparsed `"How to&nbsp;fix"` using JavaScript?

Original source