JavaScripTools Manual

JavaScriptUtil

Manipulating caret and selection on input fields

Before any caret or selection operations, the prepareForCaret function must be invoked, passing the control name or reference to it.

Important: Caret operations are supported only on Internet Explorer and Gecko based browsers (Mozilla, Firefox, Seamonkey, ...). Selection is also supported by Opera. If there is no browser support, caret operations will do nothing. There are two special functions, isCaretSupported and isInputSelectionSupported that determine if a given object supports caret and selection operations.

Then, there are several functions, which will be presented by examples.

if (!isCaretSupported('customer')) {
    alert("Your browser does not support caret operations");
}
if (!isInputSelectionSupported('customer')) {
    alert("Your browser does not support input selection operations");
}
prepareForCaret('customer');
//Set the caret position after the 3rd character
setCaret('customer', 3);
//Return the current caret position (3)
alert(getCaret('customer'));
//Set the caret before the first character 
setCaretToStart('customer');
//Set the selection to start after the 2nd character and end after the 6th
setInputSelectionRange('customer', 2, 6);
//Return an array containing 2 and 6
alert(getInputSelectionRange('customer'));
//Return the text contained on the selection
alert(getInputSelection('customer'));
//Makes the first occurrence of the string John on the input to be selected
selectText('customer', 'John');




Previous:
Manipulating options on a select box
Table of Contents Next:
Manipulating events