Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 620
Francisc Avatar asked Mar 15 '11 15:03

Francisc


People also ask

Can we use innerHTML in textarea?

For div and span, you can use innerHTML, but for textarea use value.

What is the difference between .innerHTML and .value in Javascript?

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.

Why you shouldn't use innerHTML?

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 .

Is textarea inline?

<textarea> is a replaced element — it has intrinsic dimensions, like a raster image. By default, its display value is inline-block .


2 Answers

You should use .value

myTextArea.value 
like image 117
jessegavin Avatar answered Oct 05 '22 07:10

jessegavin


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

document.getElementById('t1').innerHTML = '&lt;&gt;&amp;';  document.getElementById('t2').value = '&lt;&gt;&amp;';
<textarea id="t1"></textarea>  <textarea id="t2"></textarea>
like image 32
SuperOP535 Avatar answered Oct 05 '22 08:10

SuperOP535