开发者

JavaScript get TextArea input via .value or .innerHTML?

Is it ok to get the value of a textarea element in JavaScript with myTextArea.valu开发者_开发知识库e or should I use myTextArea.innerHTML?

Thank you.


You should use .value

myTextArea.value


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>


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>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜