20

Okay I have a list of devices where i can select which to edit. I have 3 states for the edit. When no devices are selected, when 1 device is selected or when x devices are selected.

The problem i have is when a user type some text in the textarea (commentField) and cancels the edit to edit another device, the text there was typed in the textarea won't disapere. It stays so when i get the new dialog for the new edit, the commentfield has the text from the old commentField (as if it wasn't cleared)

I have tried the following codes to remove the text (both when then cancel button is pressed and when i start a new dialog), but nothing works:

$("#commentField").text(" ");
$("#commentField").value = ' ';

Is there anyone who knows how to remove user-typed text from a textarea using jQuery??

Thanks in advance.

-Thor

Thor A. Pedersen
  • 1,122
  • 4
  • 18
  • 32
  • possible duplicate of [Set value of textarea in jquery](http://stackoverflow.com/questions/415602/set-value-of-textarea-in-jquery) – jtbandes Aug 19 '11 at 05:31

5 Answers5

65

You're looking for .val():

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

Example: http://jsfiddle.net/andrewwhitaker/q6eLV/

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • do you know why this doesn't trigger a change event in the textarea? – user3281466 Sep 15 '14 at 16:13
  • 1
    @user3281466: setting a value programatically doesn't trigger the `change` event. You could do that manually yourself if you wanted to by calling `.change` after setting the value. – Andrew Whitaker Sep 15 '14 at 16:20
3

Since textarea is a input field it has a value property so you have to use val() method. Try this

$("#commentField").val('');
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

In jQuery it's actually $("#commentField").val(" ");

Ali
  • 12,354
  • 9
  • 54
  • 83
0

You can remove text from the textarea like this:

$("#commentField").html("");

EDIT: this does not work, but I'd be really interested as to why. I always thought that the text between the textarea tags was innerHTML and html() is supposed to replace just that. Wouldn't it make sense to have textarea like input? or is it just because textareas contain long amounts of text that would look tacky between quotation marks?.

Using regular javascript with innerHTML it works for me in FF6. demo here

miken32
  • 42,008
  • 16
  • 111
  • 154
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 1
    Actually, downvoting is unfair here; I was stuck with the same problem, hence the fiddle. I also assumed that, since it was between tags, `.html()` or `.text()` would have worked, but apparently not. Maybe jQuery has a special case for `textarea` so it uses `.val()` like all other inputs. – Bojangles Aug 18 '11 at 15:09
-1

1) <textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
2) CKEDITOR.replace('editor1');
3) initiated = true;
to write your text in you text area
1)$("#editor1").val("your input value");
to clean your text area
1) $("#editor1").reset();
tank you for reading this.