Get line number, text and num-of-spaces in TextArea

by onury

HTML

<textarea id="ta" rows="10" cols="50">
First
   Second
Third
</textarea>

JavaScript

window.onload = function () {
    var ta = document.getElementById('ta');
    ta.onclick = function (e) {
        var lineNo = ta.value.substr(0, ta.selectionStart).split(/\r?\n|\r/).length;
        var lineText = ta.value.split(/\r?\n|\r/)[lineNo - 1];
        var numOfSpaces = lineText.split(/\s/).length - 1;
        console.log(lineNo, lineText, numOfSpaces);
    }
}