I'm working on a set of vanilla js functions to navigate an html textarea when the appropriate arrow button is clicked.
for example,
var text = document.getElementById('text');
function larr(){
text.focus();
var pos = text.selectionStart;
pos--
text.setSelectionRange(pos, pos);
}
<textarea id='text'></textarea>
<button onclick="larr()">←</button>
The left and right functions are simple enough, but I would also like to include up and down arrows. Since each newline can have a different amount of characters, I don't think it is as simple as setting the position forward or back one max-line-length.
I would settle for the arrows taking you to the next or previous line break. I was thinking about splitting the textarea value at the caret position and looping through characters in that direction until a \n is reached, but I can't wrap my head around it.
Does anyone have any suggestions? Thanks!
*IMPORTANT NOTE - this would be for mobile with the native keyboard hidden, so no help from the os! (unless maybe jquery trigger() or execCommand?)