Reputation: 63
<textarea name="test" id="text">
text
area
one one
two
break above
last
</textarea>
<span id="getvalues">get values</span>
$("#getvalues").click(function(){
})
How can i get all values from this textarea from each line to javascript array? This should:
Next i would like make:
$.each(textareavalues, function(index, value) {
console.log('@' + value + '@');
});
This should show me:
@text@
@area@
@one one@
@two@
@break above@
@last@
LIVE EXAMPLE: http://jsfiddle.net/BW8Z2/1/
Upvotes: 1
Views: 514
Reputation: 8930
$("#getvalues").click(function(){
var $textareavalues = $("#text").val();
var x = $textareavalues.split('\n');
$.each(x, function(index, value) {
var text = $.trim(value);
if(text !== "")
console.log('@' + text + '@');
});
});
Upvotes: 2
Reputation: 150070
When you say "ignore white space - trim", you mean "ignore leading and trailing white space on a line, but keep internal white space"? And "ignore white break" means "ignore lines with just white space"?
Something like this:
$("#getvalues").click(function(){
var lines = $("#text").val()
.replace(/\n\s*\n/g,"\n")
.replace(/^\s+|\s+$/gm,"")
.split(/\n/);
$.each(lines, function(i, val) {
console.log("@" + val + "@");
});
})
Upvotes: 4
Reputation: 38910
This will get you started:
var textareavalues = $('textarea').val().split('\n');
you can find how to trim strings and how to exclude empty items in an array in JavaScript from other questions on StackOverflow.
Upvotes: 2