hi hello or not, a...">

What value will <textarea> send and why?

<form> <textarea name="test" value="no"> hi </textarea> <input type="submit" /> </form> 

hello or not, and the reason?

I ask this question because I see javascript editors use textarea.value = html; to restore content when submitted, why do they do it if the value attribute is useless?

+6
javascript html editor textarea
source share
4 answers

hi . The value attribute for the <textarea> missing. See the W3School textarea tag for more details.

To answer the question of why you see javascript libraries for accessing the value property of the textarea DOM element, you must understand that the HTML and DOM (Document Object Model) accessed by javascript are 2 different animals. In HTML, the value of a <textarea> is its content. In the DOM, the value of the textarea node is contained in the value property. Although DOM property names often display attribute names 1: 1 in HTML, they are not always, and this is just one example.

+18
source share

In a POST response, textarea will respond with innerHTML content. However, if you set the value attribute to textarea and try to get textarea.value in JavaScript, you will get the contents of the value attribute.

The browser should never display the value textarea attribute in the physical textarea because it is not standard. The textarea tag works differently than the input tags. I would suggest that this is because the input tag does not support line breaks.

The value changed by the user will be the value of innerHTML ( html in jQuery), and not the value attribute, as is done with input fields.

+3
source share

A couple of other points -

All browsers that support javascript return read / write values ​​for textarea.value, according to the input elements. But you cannot use getAttribute ('value') or setAttribute ('value') in a text field.

You can also read the textarea type property, but the type is also not an attribute.

If you set the value property, textarea.value = string; ** it is equivalent ** textarea.appendChild (document.createTextNode (string)) -

A text field cannot contain any other elements, only text nodes.

The text in the text area specified from the value will be a literal character from the string, and setting the innerHTML property will remove any objects and minimize spaces.

+2
source share

The value of the text field is the content. The "value" property does not have a standard value for this tag, so the form or JavaScript will receive "hello" from the text field. Here is a demonstration of this in JavaScript.

 <form> <textarea name="test" value="no" id='ff'> hi </textarea> <input type="submit" /> </form> <br> <a onclick='showVal("ff")'>Show Value</a> <script> function showVal(id){ var snib=document.getElementById(id); alert(snib.value); } </script> 
0
source share

All Articles