javascript split textarea lines code example

Example 1: textarea split on newline

$(function(){
    var lines = [];
    $.each($('#data').val().split(/\n/), function(i, line){
        if(line){
            lines.push(line);
        } else {
            lines.push("");
        }
    });
    console.log(lines);
});

Example 2: textarea split on newline

<script>
$(function(){
    var lines = [];
    $.each($('#data').val().split(/\n/), function(i, line){
        if(line){
            lines.push(line);
        }
    });
    console.log(lines);
});
</script>


<textarea id="data">
I like to eat icecream. Dogs are fast.

The previous line is composed by spaces only.



The last 3 lines are empty.

One last line.
</textarea>

Tags:

Misc Example