Selection.js Save

A tiny (4kb) JavaScript cursor selection library for all browsers.

Project README


A simple (4kb) library for manipulating cursor selections across all browsers:

  • Modern browsers, using DOM Selection/Ranges
  • IE5—8, using TextRanges

This library adds a window.Selection object with static methods for retrieving, setting, and clearing a user selection. By manipulating the DOM, you can replicate most DOM Range functionality (like extract/clone/deleteContents, etc.) or just implement what your application actually needs.


Include "selection.js" in your project. The global window.Selection object is has the following static methods. As a shorthand, new Selection(window) instantiates an object with the same methods, but the window argument is no longer necessary.

  • Selection.supported -- This property is true if the browser supports the Selection object.
  • Selection.hasSelection(window) -- Returns true if anything is currently selected.
  • Selection.getOrigin(window) -- Returns an array [anchorNode, anchorOffset] of the current selection's starting anchor (in IE, returns leftmost anchor)
  • Selection.getFocus(window) -- Returns an array [focusNode, focusOffset] of the current selection's focus anchor (in IE, returns rightmost anchor)
  • Selection.getStart(window) -- Returns an array [node, offset] for the leftmost anchor.
  • Selection.getEnd(window) -- Returns an array [node, offset] for the rightmost anchor.
  • Selection.setSelection(window, originNode, originOffset, focusNode, focusOffset) -- Sets the selection to include the new origin anchor and focus anchor (in IE, origin will be leftmost anchor, focus rightmost)
  • Selection.clearSelection(window) -- Deselects all content.


Released under the MIT license.

Open Source Agenda is not affiliated with "Selection.js" Project. README Source: tcr/selection.js
Open Issues
Last Commit
10 years ago

Open Source Agenda Badge

Open Source Agenda Rating