Get current row contenteditable div?

I poked and read that there is no way to find the current line in div contenteditable, so I'm trying to write a script to best evaluate the current line.

var curLine = 1;
var curColumn = 1;

$(".editable").onkeydown = function(e) {
    var keyCode = e.keyCode || e.which;

    switch( keyCode ) {
        case 13:
            console.log("Enter; on line: " + curLine);
            curLine++;
            break;

        case 8:
            if( /* current line has a text length of 0 */ ) {
                console.log("Previous line prepended; previous enter reversed; on line: " + curLine);
                do {
                    curLine--;
                } while( curLine > 0 );
            }
            // } else if( /* single digit removed */ ) {
                // console.log("Bit prepended");
            // }
            break;
    }
};          

So, as you can see, I have a few questions. This bit: if( /* current line has a text length of 0 */ )I can’t target the current line and see if the length of the line is 0. In addition, my statement do...whiledoesn’t work, as if I press backspace on the first line on top and again, it goes into negative numbers (when it should just stay at level 1 [ > 0]). Any help would be greatly appreciated.

+3
source share
1 answer

Chrome. :

$(".editable").on("keyup", function(keyCode){
    var linesCount = $(".editable div").length + 1;
    $("#linesCount").html(linesCount + " lines.");
    return linesCount;
});

div div. div, .

http://jsfiddle.net/hescano/PypeD/

: FF <br>. .

FF: http://jsfiddle.net/PypeD/1/

0

All Articles