简体   繁体   中英

How do I get the input from a <textarea> in javascript?

The <textarea> tag doesn't have an "value" attribute so, how do I get the input from it into a JavaScript variable. For

<form name="myForm"> 
    <input type="text" name="aName">
</form> 

this is quite simple:

var javaScriptVariable = document.myForm.aName.value;

But how do I get the same result using a textarea form?

Note: Not interested in getting this on server side via php/perl etc.

I know this may sound silly for many of you, but I'm at the very beginning in the world of web programming.

<textarea id="myTextArea">Value</textarea>

var textareaVal = document.getElementById('myTextArea').value;

Demo

<textarea id='wmd-input'></textarea>

var wmd = document.getElementById('wmd-input');
var input = wmd.value;

In the case you want to use the form object, its the same really

// yes, I'm testing it on this answer's form
var form = document.getElementsByClassName('inline-post')[0];
var wmd = form['post-text'];
var input = wmd.value

Not silly, we all have to start somewhere. Try this.

<textarea rows="4" cols="50" id="myTextArea">
Hello world
</textarea>

Then to access this value:

var javaScriptVariable2 = document.getElementById("myTextArea").value;
console.log(javaScriptVariable2);

it's fine

var txtarea document.forms["myForm"].getElementsByTagName("aName").value;

alert(txtarea)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM