0

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.

3
  • No one was born a web programmer :) Commented Dec 3, 2013 at 10:43
  • use id="aName" and you will get the result using document.getElementById or document.myForm.aName.value Commented Dec 3, 2013 at 10:44
  • if your problem solved, please mark one answer to accepted answer Commented Feb 18, 2014 at 15:37

4 Answers 4

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

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

Demo

Sign up to request clarification or add additional context in comments.

Comments

0
<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

1 Comment

Thank you all, Actually it works even if there isn't a value attribute associated with textarea There was a mistake in my script I typed createNodeText() instead of createTextNode(). Finally I catched it in console. And it works. My theory :P with value attribute doesn't stand.
0

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);

Comments

0

it's fine

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

alert(txtarea)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.