JavaScript get TextArea input via .value or .innerHTML?

JavascriptTextareaInnerhtml

Javascript Problem Overview


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

Thank you.

Javascript Solutions


Solution 1 - Javascript

You should use .value

myTextArea.value

Solution 2 - Javascript

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>

Solution 3 - Javascript

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>

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFranciscView Question on Stackoverflow
Solution 1 - JavascriptjessegavinView Answer on Stackoverflow
Solution 2 - JavascriptSuperOP535View Answer on Stackoverflow
Solution 3 - JavascriptAmir Md AmiruzzamanView Answer on Stackoverflow
Solution 4 - JavascriptJimmy MarvoshView Answer on Stackoverflow
Solution 5 - JavascriptryanneufeldView Answer on Stackoverflow