[ACCEPTED]-Display current line and column number for a textarea-textarea

Accepted answer
Score: 11

When I want the current line number of textarea 2 and current column of textarea, I solved 1 like this:

<textarea  onkeyup="getLineNumberAndColumnIndex(this);" onmouseup="this.onkeyup();" >
</textarea>

function getLineNumberAndColumnIndex(textarea){
     var textLines = textarea.value.substr(0, textarea.selectionStart).split("\n");
     var currentLineNumber = textLines.length;
     var currentColumnIndex = textLines[textLines.length-1].length;
     console.log("Current Line Number "+ currentLineNumber+" Current Column Index "+currentColumnIndex );
  }
Score: 8

You may want to check out these 2 links:

http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/[The orginal source does not exist any more, the modified link points to the latest version of the Web Archive]

https://developer.mozilla.org/En/DOM:Selection

..once 4 you have a selection (cursor index in text), you 3 can split your text by newlines, thus getting 2 line number. you can get column by determining 1 index from beginning of a line

More Related questions