tags:

views:

276

answers:

4

lets say inside a <textarea>, i type in a bunch of keywords on each new line.

keyword1
keyword2
keyword3
...




$('textarea[name=sometextarea]').val().split('\n').each(function(e){
alert($(this));     
});
+3  A: 

I think your problem is with the jquery object. val() returns a string and split() will return an Array. But each() is a property of the JQuery object. Try this instead:

$($('textarea[name=sometextarea]').val().split('\n')).each(function(e){
alert($(this));                                 
});

notice the extra $(...) around the the split() return.

fserb
The $().each() method is intended for iterating over elements, the $.each() method is recommended for iterating over other objects, like an array of strings.
Guffa
+3  A: 

The array object doesn't have any each method. As you are looping strings and not elements, use the jQuery.each method (instead of the jQuery().each method):

var lines = $('textarea[name=sometextarea]').val().split('\n');
$.each(lines, function(){
  alert(this);
});
Guffa
this returns [Object object] and not the string.i tried this.text(), and it says no method error.
aoghq
its says this.text() is not a function.
aoghq
@aoghq: jQuery casts the value to Object for some reason. You can use String(this) to get the value cast back to string.
Guffa
A: 

Like this...

$.each($('textarea[name=sometextarea]').val().split('\n'), function(e){
alert(this);                                 
});
Sverre Ølnes
A: 

jQuery provides an each method on the jQuery (or $) object. This should be used, rather than creating a jQuery object out of an array:

$.each($('textarea[name=sometextarea]').val().split('\n'), function(e){
    alert(this);
});
Rudd Zwolinski