jQuery - remove user input text from textarea
Asked Answered
K

5

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

Kwabena answered 18/8, 2011 at 14:46 Comment(1)
possible duplicate of Set value of textarea in jqueryStylistic
D
65

You're looking for .val():

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

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

Dipole answered 18/8, 2011 at 14:48 Comment(2)
do you know why this doesn't trigger a change event in the textarea?Velmaveloce
@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.Dipole
K
3

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

$("#commentField").val('');
Kelikeligot answered 18/8, 2011 at 14:51 Comment(0)
I
1

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

Ithaca answered 18/8, 2011 at 15:3 Comment(0)
P
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

Pigg answered 18/8, 2011 at 14:49 Comment(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.Missilery
L
-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.

Luciolucita answered 13/8, 2016 at 4:41 Comment(1)
The question does not reference CKEditor.Otherdirected

© 2022 - 2024 — McMap. All rights reserved.