JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id="test" rows="5" cols="50">
Some text followed by a line break
</textarea>
<input type="button" onmousedown="alert(isCaretAtTheEnd(document.getElementById('test')))" value="Is caret at the end?">
JavaScript
function isCaretAtTheEnd(el) {
var valueLength = el.value.length;
if (typeof el.selectionEnd == "number") {
// Modern browsers
return el.selectionEnd == valueLength;
} else if (document.selection) {
// IE < 9
var selRange = document.selection.createRange();
if (selRange && selRange.parentElement() == el) {
// Create a working TextRange that lives only in the input
var range = el.createTextRange();
range.moveToBookmark(selRange.getBookmark());
return range.moveEnd("character", valueLength) == 0;
}
}
return false;
}