Francisc
Francisc

Reputation: 80475

JavaScript get TextArea input via .value or .innerHTML?

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

Thank you.

Upvotes: 61

Views: 115491

Answers (5)

SuperOP535
SuperOP535

Reputation: 229

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>

Upvotes: 17

Jimmy Marvosh
Jimmy Marvosh

Reputation: 11

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

Upvotes: 1

Amir Md Amiruzzaman
Amir Md Amiruzzaman

Reputation: 2079

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>

Upvotes: 6

ryanneufeld
ryanneufeld

Reputation: 157

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.

Upvotes: 0

jessegavin
jessegavin

Reputation: 75650

You should use .value

myTextArea.value

Upvotes: 92

Related Questions