Last active
October 30, 2015 14:45
-
-
Save wizard04wsu/8007581083970ee40a30 to your computer and use it in GitHub Desktop.
Go to a specified line & character within a text box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Go To Line/Character</title> | |
</head> | |
<body> | |
<form action="javascript:go();"> | |
<textarea id="txt" rows="25" cols="150"></textarea><br> | |
<br> | |
<label>Line <input type="number" id="line" step="1" min="1" value="1"></label><br> | |
<label>Char <input type="number" id="char" step="1" min="1" value="1"></label><br> | |
<input type="submit" id="go" value="Go"> | |
</form> | |
<script type="text/javascript"> | |
function go(){ | |
var txt = document.getElementById("txt"), | |
line = document.getElementById("line").value, | |
char = document.getElementById("char").value, | |
rxp, before, loc; | |
rxp = new RegExp("^(.*\n){"+(line-1)+"}"); | |
before = rxp.exec(txt.value); | |
if(!before){ //the text has fewer than `line` lines | |
//put the cursor at the end of the text | |
before = txt.value; | |
char = 0; | |
} | |
else{ | |
before = before[0]; | |
char--; | |
} | |
loc = before.length + 1*char; | |
txt.setSelectionRange(loc, loc+1); | |
txt.focus(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment