JQuery textarea value is empty

This type of question tends to get a negative vote immediately. If you have an answer, please help! Thank.

I have a script that adds textarea to dom. When I enter data and later try to restore it, the value is returned empty.

This sets the textarea value successfully:

$("textarea").each(function(){
    $(this).val("ASDFASDF");
});

      

This successfully sets the css background property for the textarea:

$("textarea").each(function(){
    $(this).css({"border":"2px solid red"});
});

      

But this doesn't return a value:

$("textarea").each(function(){
    alert($(this).val());
});

      

I've tried .text (),. Html (),. Value - they all return nothing! It doesn't warn about "undefined" or is null, just empty.

I can't figure out why.

Thanks in advance for your suggestions.

ADDITIONAL INFORMATION

HTML:

<textarea id="Bio" rows="4"></textarea>

      

JQuery library: http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js

The function is launched by the button after loading the dom.

A simplified version of the code is given here: http://social-gamer.com/textarea.html

+3


source to share


3 answers


It works, something else is a problem. You can see how it works here

<textarea>Hello One</textarea>
<textarea>Hello One two</textarea>
<textarea>Hello One two three</textarea>

$("textarea").each(function(){
    alert($(this).val());
});

      

  • Which version of jQuery are you using?
  • Does your code get executed before the DOM is loaded?
  • What does your HTML code look like? Are you assigning the value attribute to the textbox? It doesn't work, see http://jsfiddle.net/2bjLJ/1/

Other Notes

Not necessary each

if you want to call a method on all base objects.



$("textarea").each(function(){
    $(this).val("ASDFASDF");
});

$("textarea").each(function(){
    $(this).css({"border":"2px solid red"});
});

      

It can only be

$("textarea").val("ASDFASDF");
$("textarea").css({"border":"2px solid red"});

      

However, when reading it, you need to call each one, otherwise you will get the value from the first object in the jQuery collection

Working example

+4


source


The problem turned out to be double identification. Normally I would check this first, BUT I didn’t even refer to the element by its ID, but simply by the "textarea" input type.



So, even when targeting an element by class or simply by its position in the DOM, if it has a duplicate ID it won't work. I didn't really think about it before because I didn't get by ID!

+4


source


how are you,

Listen, the TextArea is the only one of the inputs that doesn't get and set the value with val (), because when you put a value in a textarea, you put it inside a tag.

Here's an example

<textarea>Inside is the content so to 
get the content inside of tags you use HTML or TEXT</textarea>

      

So when you are trying to get data, you should do this:

$('textarea').text();

      

And to set the data of the textbox this:

$('textarea').text("HERE THE CONTENT.");

      

Here is a violin for you to see everything I say

http://jsfiddle.net/nfTVJ/

Good luck and goodbye

-1


source







All Articles