views:

57

answers:

3

I have a on a form of mine. I´m trying to reset the value (the value typed by the user) of this field on a certain event. However, I seem unable to access it both with .val() and .html().

Any advice on this?

+1  A: 

Set an id on that field so you can easily target it with $('#the_id'). If it's a textarea use .text(''), input use .val('')

baloo
Always use `text()` when setting text, not `html()`. jQuery will attempt to parse valid HTML from the argument supplied to `html()`, which is not only slower but can also cause undesired effects when using `<` and `>` in the text.
Andy E
@Andy thanks, edited my answer
baloo
yes... but also, do not use `.text()` on textarea... especially getting the value... use `.val()` instead... http://jsbin.com/afuja3/2/
Reigel
+4  A: 

Do not use .html(), use .val().

$("#your_textarea_id").val("Some literal <textarea> content.");

This way special characters (<, > etc) will show up correctly.

Tomalak
if I were you I would not suggest using `.text()` on <textarea>... http://jsbin.com/afuja3/2/
Reigel
@Reigel: *Setting* a value with `text()` works perfectly fine. However, using `.val()` is probably still the better idea. My point was more about not to use `.html()`. (Code sample corrected, thanks for the hint.)
Tomalak
+1  A: 
$('#text_area').val("");
newinjs