tags:

views:

697

answers:

5

How would I change the content of a <textarea> element with JavaScript?

I want to make it empty.

A: 

If its Jquery ..

$("#myText").val('');

or

document.getElementById('myText').value = '';

http://www.hscripts.com/tutorials/javascript/dom/textarea-events.php

Wbdvlpr
JavaScript.... Not Jquery...
ian
JQuery is JavaScript, it's a framework.
Charlie Somerville
+8  A: 

Like this:

document.getElementById('myTextarea').value = '';

or like this in jQuery:

$('#myTextarea').val('');

Where you have

<textarea id="myTextarea" name="something">This text gets removed</textarea>

For all the downvoters and non-believers:

Greg
Textara has no value property!
Petr Peller
Didn't think value would work cause there is no. value= in it but thanks!
ian
@Petr Peller yes it does...
Greg
@Greg: Ok, you're right :)
Petr Peller
does not work in internet explorer on windows mobile devices.
Paul
@Paul What version?
Greg
A: 

Use innerHTML property.

textarea = document.getElementById('myArea');
textarea.innerHTML = "";

EDIT:

I just checked that out and both the .innerHTML and the .value works in FF 2.0. But i think use of the .innerHTML (or an .innerTEXT eventually) makes much more sense according to there is no value property for textarea tag in HTML.

Petr Peller
value not innerHTML
Greg
Tried that originally does not work.
ian
Thats strange because in HTML there is no value property in textarea. I'm gonna try that.
Petr Peller
There is a value property. There isn't a value *attribute*. This is DOM, not HTML. http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-70715579
David Dorward
I don't say that there is no value property in DOM. I just say that it's not in HTML. So I wouldn't expect it in the DOM.I would like to see any compatibility or functional comparsion between innerHTML and value properties.
Petr Peller
+1  A: 

If you can use jQuery, and I highly recommend you do, you would simply do

$('#myTextArea').val('');

Otherwise, it is browser dependent. Assuming you have

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

In most browsers you do

myTextArea.innerHTML = '';

But in Firefox, you do

myTextArea.innerText = '';

Figuring out what browser the user is using is left as an exercise for the reader. Unless you use jQuery, of course ;)

Edit: I take that back. Looks like support for .innerHTML on textarea's has improved. I tested in Chrome, Firefox and Internet Explorer, all of them cleared the textarea correctly.

Edit 2: And I just checked, if you use .val('') in jQuery, it just sets the .value property for textarea's. So .value should be fine.

Aistina
.value = ''; works in Chrome FF and Safari... .innerHTML does not work in Chrome did not test in others.
ian
+1  A: 

Although many correct answers have already been given, the classical (read non-DOM) approach would be like this:

document.forms['yourform']['yourtextarea'].value = 'yourvalue';

where in the HTML your textarea is nested somewhere in a form like this:

<form name="yourform">
    <textarea name="yourtextarea" rows="10" cols="60"></textarea>
</form>

And as it happens, that would work with Netscape Navigator 4 and Internet Explorer 3 too. And, not unimportant, Internet Explorer on mobile devices.

Paul