Skip to content

Instantly share code, notes, and snippets.

@jobyjoseph
Last active November 26, 2015 06:14
Show Gist options
  • Save jobyjoseph/72060824a5169885a137 to your computer and use it in GitHub Desktop.
Save jobyjoseph/72060824a5169885a137 to your computer and use it in GitHub Desktop.
SetCaretAtEnd function accepts a javascript dom object and sets cursor to end.
function SetCaretAtEnd(elem) {
var elemLen = elem.value.length;
// For IE Only
if (document.selection) {
// Set focus
elem.focus();
// Use IE Ranges
var oSel = document.selection.createRange();
// Reset position to 0 & then set at end
oSel.moveStart('character', -elemLen);
oSel.moveStart('character', elemLen);
oSel.moveEnd('character', 0);
oSel.select();
}
else if (elem.selectionStart || elem.selectionStart == '0') {
// Firefox/Chrome
elem.selectionStart = elemLen;
elem.selectionEnd = elemLen;
elem.focus();
} // if
};
SetCaretAtEnd($('.m-status-item .editPostInput')[0]); // This is how a jQuery object is passes. note [0].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment