1

Given this div on an html page :

<div id="div1"&gt <table&gt fred </div&gt

How can I use javascript to extract the contents of "div1" into a string, so that the string has this value :

" <table&gt fred "

Using code such as :

var a = document.getElementById('div1').innerHTML;

is unsuccessful, because the browser adds markup to the string to try to create valid html table markup.

Neil Eames
  • 21
  • 2
  • 1
    You can't do that. Because there is no such thing as HTML string, there is DOM tree, which has been built and fixed to have a valid structure. – dfsq Oct 27 '14 at 14:41
  • 1
    You would need to request the markup from source and parse it as a string – Alex K. Oct 27 '14 at 14:48

4 Answers4

2

As written in two other threads (Get exact html() with closing tag for which starting tag is missing and Use javascript to get raw html code), the only way to achieve what you want is by means of AJAX.

bobince phrased it pretty well:

If you really really need to get your page's actual source HTML, you can make an XMLHttpRequest to your own URL (location.href) and get the full, unparsed HTML source in the responseText. There is almost never a good reason to do this.

Community
  • 1
  • 1
Alexander Jank
  • 2,440
  • 2
  • 18
  • 19
0

Using innerHTML it's fine, but if you don't want the browser to fix your markup for you, you should use:

<div id="div1"> &lt;table&gt; fred </div>

Otherwise you're writing invalid HTML

nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
0

There is no way for it to ignore incorrect markup. Either fix your markup or use &lt; and &gt; instead of < and >.

Source: Why is my (incorrect) DOM structure auto-corrected after JQuery DOM manipulation?

Community
  • 1
  • 1
ntzm
  • 4,663
  • 2
  • 31
  • 35
-3

Just use Jquery:

$('#div1').html()
Thermatix
  • 2,757
  • 21
  • 51
  • But this is javascript – asdf_enel_hak Oct 27 '14 at 14:34
  • Jquery is a java-script library that makes things like this MUCH easier. – Thermatix Oct 27 '14 at 14:35
  • 1
    This does not answer the question, he is asking how to get the inner HTML without markup correction. This function returns the corrected markup. On top of that, he did not tag the question with jQuery, so you should not be giving jQuery answers. – ntzm Oct 27 '14 at 14:37
  • This answer doesn't make any sense. `html` just returns `innerHTML` and it won't solve the OP problem – nicosantangelo Oct 27 '14 at 14:37
  • Fair enough,I just thought, inventing the wheel and all, still I wasn't aware that it just returns `innerHTML`. – Thermatix Oct 27 '14 at 14:39
  • To be fair, it does some other stuff, but in most cases it does. – nicosantangelo Oct 27 '14 at 14:40
  • Adding any library to any project should be done with careful consideration. It isn't actually saving you any lines of code -- each library may be thousands of lines of code that *you didn't write*. That adds another variable to your project and your environment, and another code dependency. Well-established libraries like jQuery aren't, in balance, an exceptional risk, but you shouldn't add it just to accomplish one simple, basic goal. If this is the only thing javascript is doing, adding jQuery is a bad recommendation. That's why, without context, you shouldn't make that suggestion. – Chris Baker Oct 27 '14 at 15:32