With textarea it is quite straightforward:
<textarea id='elememt'>
1. Blah blah blah.
<div>2. Header</div>
3
</textarea>
var lines = $('textarea').val().split('\n');
alert(lines[0]; // show the first line "1. Blah blah blah."
alert(lines[2]; // show the third line "3"
But how do I do the same with a contenteditable DIV?
I've looked at this:
var first_line = $("#element")
.contents()
.filter(function() {
return !!$.trim( this.innerHTML || this.data );
})
.first();
Which gets the first line but how do I get the third line and so on?
You can use
.html(),String.prototype.match()withRegExp/.*\n/gto match one or more characters including newline character,String.prototype.replace(),Array.prototype.map(),Array.prototype.join()to return original matches, replacing string or part of string at selected index of matched lines.Given
htmlat Question, the first element within the array returned by.match()will be a newline character; use3to match fourth element of returned array.