Professional Documents
Culture Documents
range of <input> and <textarea> elements, as well as highlight text on the page.
Browser Compatibility
Tested in:
• IE6+
• Chrome
• Firefox
Features
• Get/set cursor position and selected range
• Insert text at cursor position
• Replace selected range with text
• Handles differences in line endings between browsers
• Select/deselect all text within any element
API
Caret
$.fn.caret()
Interrogate and manipulate the cursor position of an input field at a single point without
selecting any text.
Examples:
Examples:
Range
$.fn.range()
Interrogate and manipulate the selected range of an input field.
• startPos Number: New selection start position. Zero-based index relative to the
beginning of the input's value. Negative numbers are relative to the end of the
input's value.
• endPos Number (optional): New selection end position. Zero-based index relative to
the beginning of the input's value. Negative numbers are relative to the end of
the input's value. If omitted, defaults to value.length.
Examples:
// Select everything after the 3rd character through (and including) the 8th
character
$('input').range(3, 8);
$('textarea').val('Hello\nWorld').range(3, 8).range().text === 'lo\nWo';
// Select the 8th-last character up to (but NOT including) the 3rd-last character
$('input').range(-8, -3);
$('textarea').val('Hello\nWorld').range(-8, -3).range().text === 'lo\nWo';
.range(text) returns jQuery object
Replace the currently selected text of the first matched element with the given text and
select the newly inserted text.
Examples:
$('input').range('Replacement Text');
Select All
$.fn.selectAll()
Selects all text in any element (div, span, input, textarea, label, etc.).
.selectAll() returns jQuery object
Deselect All
$.deselectAll()
Deselects all text on the page.
$.deselectAll() returns jQuery function
Types
Range:
{
start: Number,
end: Number,
length: Number,
text: String
}
Technical Notes
Line Endings (a.k.a. newlines)
IE and Opera handle line endings differently than Chrome, Safari, and Firefox.
IE8 and Opera 9 on Windows use \r\n. All the other browsers I tested (Safari 4 and
Firefox 3.5 on Windows, and Firefox 3.0 on Linux) use \n. They can all handle \n just fine
when setting the value, though IE and Opera will convert that back to \r\n again
internally. There's a SitePoint article with some more details called Line endings in
Javascript.
Note also that this is independent of the actual line endings in the HTML file itself
(both \n and \r\n give the same results).
When submitting the form, all browsers canonicalize newlines to \r\n (%0D%0A in URL
encoding).
.caret() and .range() smooth out these differences for you by normalizing line endings
so they behave properly in all browsers. More specifically, they strip \r characters so that
newlines are always represented by a single \n character. As a result, positioning the
caret before or after a newline works the way you expect without any fuss.
IMPORTANT: Always access input/textarea values using jQuery's .val() method
instead. DO NOT use the browser's native .value property. Doing so will bypass newline
normalization and return strings containing \r in IE and Opera which will almost
certainly screw up length and position calculations.
Here's a quick test you can do to see if your browser normalizes line endings to \n: