1

I have some TextAreas

<textarea>Default Value</textarea>
<textarea>Default Value</textarea>

The user enters some text and I want to get their NEW values in JavaScript:

$('textarea').each(function(index, item) {
    alert(item.value);
});

It sounds simple, but all I get is "Default Value".

I've tried:

item.nodeValue, 
item.textContent, 
item.innerHTML. item.innerText, 
item.value, 
$(item).val() in jQuery

I'm always getting "Default Value". Though firebug even shows the new value in HtmlTextAreaElement => value. That should be item.value, right? I feel stupid, please help me.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user1446186
  • 11
  • 1
  • 5
  • Take a look to: http://stackoverflow.com/questions/144810/jquery-get-textarea-text – Alberto Solano Jun 09 '12 at 12:49
  • 2
    [Your code already works fine.](http://jsfiddle.net/aNzzG/) – Pointy Jun 09 '12 at 12:49
  • do you tried `$(this).val()`? – ZiTAL Jun 09 '12 at 13:01
  • Okay thanks for pointing that out, now I know what the problem is: My TextAreas are nested into some containers, what I'm trying to access is actually a clone of their 2nd parent. jQuery seems to lose all the values in the process, even with .clone(true, true); – user1446186 Jun 09 '12 at 13:04

3 Answers3

0

in textarea it's not item.value, it's innerHTML

jquery's val() works though

<input type="text" value="some text" />
<textarea id="something" name="something">some text</textarea>

you can do:

$('textarea').each(function(index, item){
    console.log($(item).val());
});
galchen
  • 5,252
  • 3
  • 29
  • 43
  • 3
    This is completely incorrect. The "value" attribute of a ` – Pointy Jun 09 '12 at 12:48
0

Give your textareas a "name" attribute

<textarea name="name">innterHTML</textarea>

and use

item.innerHTML

as suggested, for textarea it is innerHTML, not value :)

PsychoMantis
  • 993
  • 2
  • 13
  • 29
0

The user enters some text and I want to get their NEW values in JavaScript:

This should do it:

$('textarea').keyup(function(){
   alert(this.value);
});

To read already entered value (not when user types it), use:

$('textarea').each(function(index, item) {
    alert(this.value);
});
Sarfraz
  • 377,238
  • 77
  • 533
  • 578