To set Caret Position at the end of text in textbox or textarea

jQuery.fn.putCursorAtEnd = function() {
return this.each(function() {
// Cache references
var $el = $(this),
el = this;
// Only focus if input isn’t already
if (!$el.is(“:focus”)) {
$el.focus();
}
// If this function exists… (IE 9+)
if (el.setSelectionRange) {
// Double the length because Opera is inconsistent about whether a carriage return is one character or two.
var len = $el.val().length * 2;
// Timeout seems to be required for Blink
setTimeout(function() {
el.setSelectionRange(len, len);
}, 1);
} else {
// As a fallback, replace the contents with itself
// Doesn’t work in Chrome, but Chrome supports setSelectionRange
$el.val($el.val());
}
// Scroll to the bottom, in case we’re in a tall textarea
// (Necessary for Firefox and Chrome)
this.scrollTop = 999999;
});
};

This sets the cursor or caret at the end but doesn’t scroll that caret/cursor into view… to achieve that u shall use text_box.scrollLeft = 999999 which scrolls the view into cursor.
Before setting the scrollLeft, ensure that the focus is set on the input field

Advertisements

Window.load Vs document.load

Window loads after document
Window includes HTML code + associated elements like images.

If multiple document.load and window.load is written then the order of the script will be taken into account by the browser.

Auto Scoll for div

advanced:{ autoScrollOnFocus: “input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable=’true’], div” } –> div will not be available by default.

External Supporting info:

jQuery custom content scroller

[I am not the owner of any of the mentioned source code ]

Inserting a character into text field

Link : rangyinputs-jquery-src.js  to the html page. (I am not the author.)

Download link : https://github.com/timdown/rangyinputs (I don’t own this account.)

To insert, use the following command:

$(“#inputTextBox”).insertText(a, caretPosition , “collapseToEnd”);

a –> to contain the character to be inserted

caretPosition –> position at which the character has to be inserted.

collapseToEnd –> cursor to be placed at the end of inserted text