npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jquery-caret-plugin

v1.5.3

Published

jQuery Caret Plugin ===================

Downloads

3,691

Readme

jQuery Caret Plugin

v1.5.2

Cross-browser jQuery plugin that allows you to manipulate the cursor position and selection 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.

.caret() returns Number

Get the cursor position of the first matched element. If one or more characters are selected, the start position of the selected range is returned.

.caret(pos) returns jQuery object

Set the cursor position of the first matched element.

  • pos Number: New cursor 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.

Examples:

// Place cursor after the 3rd character
$('input').caret(3);
$('input').val('Hello World').caret(3).caret() === 3;

// Place cursor before the 3rd-last character
$('input').caret(-3);
$('input').val('Hello World').caret(-3).caret() === 8;

.caret(text) returns jQuery object

Insert text at the current cursor position of the first matched element and place the cursor after the inserted text.

  • text String: Text to insert at the current cursor position.

Examples:

// Insert some text at the current cursor position
$('input').caret('Inserted Text');
$('input').val('Held').caret(2).caret('llo Wor').val() === 'Hello World';
$('input').val('Held').caret(2).caret('llo Wor').caret() === 9;

Range

$.fn.range()

Interrogate and manipulate the selected range of an input field.

.range() returns Range

Get the selected range (start and end position) of the first matched element, along with the value of the selected text and its length. If no text is selected, the start and end position will be equal to the cursor position returned by .caret().

.range(startPos [ , endPos ]) returns jQuery object

Set the selection range of the first matched element.

  • 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 6th character
$('input').range(6);
$('textarea').val('Hello\nWorld').range(6).range().text === 'World';

// 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 everything 5 characters from the end and later
$('input').range(-5);
$('textarea').val('Hello\nWorld').range(-5).range().text === 'World';

// 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.

  • text String: Text to replace the current selection with.

Examples:

$('input').range('Replacement Text');

$('textarea').val('Hello\nWorld').range(0, 5).range('Goodbye').val() === 'Goodbye\nWorld';
$('textarea').val('Hello\nWorld').range(5, 6).range(' - ').val() === 'Hello - World';

// Same as inserting text via $('input').caret(2).caret('llo Wor')
$('input').val('Held').range(2, 2).range('llo Wor').val() === 'Hello World';

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.

From JavaScript string newline character? on Stack Overflow:

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:

// true in Chrome, Safari, and Firefox
// false in IE and Opera on Windows
var normalizesNewlines = (function () {
    var textarea = document.createElement('textarea');
    textarea.value = '\r\n';
    return textarea.value === '\n';
}());

Detached Elements

It is not possible to set the selection range/caret position of a detached element in any browser. The element must be in the DOM to be able to manipulate its caret/range.

Wrong:

// false in all browsers
$('<input/>').val('abcdef').caret(3).caret() === 3;

Right:

// true in all browsers
$('<input/>').appendTo($('body')).val('abcdef').caret(3).caret() === 3;

Selection State

All browsers except IE track selection range on a per-element basis. In other words, each element remembers its own selection range and caret position so that switching focus from one input to another doesn't lose the original input's selection range/caret position.

Predictably, IE only has a single global selection range for the entire page and can only track one element at a time. Thus placing the cursor in the middle of an input and pressing Tab followed by Shift + Tab loses the original input's caret position.

While not necessarily a huge issue, it's something to be aware of.

Browser Bugs

Deselecting Text in IE 6-10

In all versions of IE (6-10), clicking inside selected text returns the wrong selection range and caret position.

Run test/manual/newline-range.html in any version of IE and select some text in the textarea. Then click anywhere inside the selection (without moving the mouse) to see the bug in action.

Clicking outside the selection works just fine.

License

MIT license. See MIT-LICENSE.txt.