Lets say I have a textarea with this text:
- first line some text.
- second line, other text. next line will be empty.
- (empty line)
- (empty line)
- last line here
As you can see, lines 3 and 4 are empty (\n
). I need to get the exact lines structure (with empty lines as well) and convert it to an array. Each line is an element of the array. This is my current code:
var lines = $('#q').val().split(/\n/);
alert(lines.length); //using alert() because IE doesn't support console.log()
var texts = [];
for(i = 0; i < lines.length; i++) {
texts.push($.trim(encodeURIComponent(lines[i])));
}
It works great on all browsers, except IE. For some reason, the split() function ignores empty lines (3 and 4) in IE. Because of that, they are never passed into the array :s
Solution by Squeegy in the comments
Replace split(/\n/)
with split("\n")
- damn you IE!