60

Is it ok to get the value of a textarea element in JavaScript with myTextArea.value or should I use myTextArea.innerHTML?

Thank you.

Francisc
  • 77,430
  • 63
  • 180
  • 276

5 Answers5

92

You should use .value

myTextArea.value
jessegavin
  • 74,067
  • 28
  • 136
  • 164
  • 5
    Since textarea has an endtag .I thought i have to use .innerHTML & .value won't work.I was setting textarea value in codebehind.Though innerHTML was giving me value but even though i edit the value ,it use to give me the same old value that i had setup from code behind.Read ur answer and used textarea.value.its working fine now.I'm getting textarea value properly now.up voted d ans. – Neelam Jun 16 '12 at 09:03
  • 4
    Please, for this kind of questions always point the spec, thanks: html.spec.whatwg.org/multipage/forms.html#dom-textarea-value – Fagner Brack Oct 25 '14 at 00:47
17

One difference is that you can use HTML entities with .innerHTML

document.getElementById('t1').innerHTML = '<>&';
document.getElementById('t2').value = '<>&';
<textarea id="t1"></textarea>
<textarea id="t2"></textarea>
SuperOP535
  • 229
  • 3
  • 4
6

For div and span, you can use innerHTML, but for textarea use value. Please see the example below.

<script language="javascript/text">
document.getElementById("spanText").innerHTML ="text";
document.getElementById("divText").innerHTML ="text";
document.getElementById("textArea").value ="text";
</script>


<span id="spanText"></span>
<div id="divText"></div>
<textarea id="textArea"></textArea>
Amir Md Amiruzzaman
  • 1,911
  • 25
  • 24
1

Don't use innerHTML use value e.g. document.getElementById(name).value

0

The answer depends on your situation.

I would personally use .value as that's what the other form inputs provide. It's easier to be in the habit of doing it that way.

ryanneufeld
  • 157
  • 1
  • 4