Skip to content

Instantly share code, notes, and snippets.

@hbt
Created December 25, 2016 00:01
Show Gist options
  • Save hbt/74586d1ff130a3cd00477835352a14f5 to your computer and use it in GitHub Desktop.
Save hbt/74586d1ff130a3cd00477835352a14f5 to your computer and use it in GitHub Desktop.
<!-- view ruby vrome -->
<textarea onkeyup="getLineNumberAndColumnIndex(this);" id="txt" rows="14" cols="60" style="background-color: black; color: white;">
asdsadh
das
d
d
as
dsa
d
asdas
</textarea>
<script>
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
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 );
}
setCaretToPos(document.getElementById('txt'), 14, 10);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment