Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textarea doesn't update

With jQuery, I added a textarea

$('#description').append('<textarea rows="8" cols="40">test</textarea>');

Ok, and I change "test" text to "test 01". But, when I try

 var cadena = $('#description').text();
 alert(cadena);

I get "test" and not "test 01". Why?

like image 257
JuanPablo Avatar asked Jan 31 '12 03:01

JuanPablo


2 Answers

give the textarea an id and then change it's value with the val function:

$('#description').append('<textarea id="xxx" rows="8" cols="40">test</textarea>');    
$('#xxx').val('test01');

//... later on
alert($('#xxx').val());
like image 118
gdoron is supporting Monica Avatar answered Sep 28 '22 09:09

gdoron is supporting Monica


Like it says on the .text() doco page, "To set or get the text value of input or textarea elements, use the .val() method."

You don't say how you change the text, but you should be doing this:

$('#description textarea').val("test 01");        // change the text

var cadena = $('#description textarea').val();   // retrieve the current text

Noting that "#description" is the textarea's container, and you need to select the textarea itself to get the value. Of course the above won't work if you have more than one textarea in that container, so it would be better if you could assign the new textarea an id an then select based on that.

like image 30
nnnnnn Avatar answered Sep 28 '22 10:09

nnnnnn