test'); Ok, and I c...">

Textarea does not update

With jQuery I added a text box

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

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

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

I get a "test", not a "test 01". Why?

+8
jquery jquery-selectors textarea
source share
4 answers

enter the textarea identifier and then change it using the val function:

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

As the doco .text() page says , "To set or get the text value of input or textarea elements, use the .val() button.

You do not say how you change the text, but you must do this:

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

Noting that "#description" is a textarea container, you need to select a text field to get the value. Of course, the above will not work if there is more than one text field in this container, so it would be better if you could assign a new text area to id and then select based on this.

+8
source share

It looks like you want to do this:

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

And then this:

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

Otherwise, you just add textarea inside any #description , and then try to get the .text value ... whatever (whatever that is).

If you add id to textarea , you can be more specific with your choice and get the text value you are looking for.

+3
source share

User .val() instead of .text() .

If you have only one text area , you can do

  var cadena = $('#description textarea:first-child').val(); 

Test case: http://jsfiddle.net/UzK59/

OR

If you add more than one text area

  var counter = 0; $('#description').append('<textarea id="textArea'+ counter +'" rows="8" cols="40">test</textarea>'); counter++; 

and then

  $('#textArea' + counter).val(); //Considering counter is 0 (Zero) here 

Test case: http://jsfiddle.net/UzK59/1/

To set the value you can do

  $('#textArea' + counter).val('New Value') 

Hope this helps you.

+3
source share

All Articles