Reputation: 1447
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));
});
Upvotes: 12
Views: 25325
Reputation: 39
$("#your-text-area").live("keypress",function(){
var lines = $(this).val().split("\n");
$.each(lines, function(n, elem) {
console.log(elem);
$("<li></li>").text(elem).appendTo($("#target"));
});
You can "listen" using live on keypress, as the others suggested split by newline and the trick in the loop is that with "each" you get an index and value (elem in the code). This snippet appends the line value to another target element which you can empty before looping using $("#target").empty();
Upvotes: 1
Reputation: 885
I have solved this with codeigniter and php. Here is a code:
$this->load->helper('text');
$name = 'some text that is longer than the width of text area and you need to do some thing with it...';
$name_array = word_wrap($name, 20); //20 is a number of character where your text will be wrapped.
foreach(preg_split("/((\r?\n)|(\r\n?))/", $name_array) as $line){
echo $line.'</br>';
//from here, you can do what ever you want with $line, put it to another array..whatever
}
Upvotes: -1
Reputation: 700312
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);
});
Upvotes: 23
Reputation: 27581
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);
});
Upvotes: 1
Reputation: 126
Like this...
$.each($('textarea[name=sometextarea]').val().split('\n'), function(e){
alert(this);
});
Upvotes: 0
Reputation: 4223
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.
Upvotes: 3