As you're always going to be checking for the last character, you could grab the length
property of the textarea
value
on every keydown
and compare the length
with the textarea's selectionEnd
property:
selectionEnd
- Specifies or returns the end position of the selected text within the current element.
Example:
document.getElementById("textAreaIdHere").onkeydown = function(){
// Get length of textarea value
var len = this.value.length;
// If character entered is at the end of the textarea (therefore cursor)
if(this.selectionEnd === len) {
// Is at the end
}
}
selectionEnd
is only available to use as of IE9+. For implementing this feature in older browsers, this shim may be of use.