Is it ok to get the value of a textarea element in JavaScript with myTextArea.value
or should I use myTextArea.innerHTML
?
Thank you.
For div and span, you can use innerHTML, but for textarea use value.
value gives you the currently-set value of a form element (input, select, textarea), whereas . innerHTML builds an HTML string based on the DOM nodes the element contains.
The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies. You can read the MDN documentation on innerHTML .
<textarea> is a replaced element — it has intrinsic dimensions, like a raster image. By default, its display value is inline-block .
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With