4

I have a very simple test here:

var cells = document.querySelectorAll('textarea#test');

        for (var i = 0, il = cells.length; i < il; i++) {
          cell = cells[i];
          cell_contents = cell.innerHTML;
          console.log(cell_contents);
        }
<form>
  <textarea id="test" style="width:600px;">
    <script>
  </textarea>
</form >

The console.log shows:

&lt;script&gt;

I can't work out why that is? I'd like to know if the textarea contains HTML using Javascript, but whenever I check the contents of it, the HTML is always encoded.

I had a look at this SO question: does <textarea> auto-encode inner html?

But am none the wiser.

wscourge
  • 10,657
  • 14
  • 59
  • 80
4532066
  • 2,042
  • 5
  • 21
  • 48
  • Try `cell.innerText` if you want it to output ` – George Feb 15 '17 at 14:52
  • Thanks George - I tried that, it then returns no data. I found this http://stackoverflow.com/questions/19030742/difference-between-innertext-and-innerhtml-in-javascript before, which says it's not supported... – 4532066 Feb 15 '17 at 14:55
  • 3
    You should use the `.value` of the textarea. –  Feb 15 '17 at 14:55
  • 1
    @squint thanks - that fixed it. – 4532066 Feb 15 '17 at 14:57
  • you could simply debug this by looking at the prototype chain. (**sug for future) – m87 Feb 15 '17 at 15:02

1 Answers1

4

Try using .value instead.

document.getElementById("b").innerText = document.getElementById("a").value
<textarea id="a"><script></textarea>
<p id="b"></p>
wscourge
  • 10,657
  • 14
  • 59
  • 80