Selection.modify()

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The Selection.modify() method applies a change to the current selection or cursor position, using simple textual commands.

Syntax

sel.modify(alter, direction, granularity)

Parameters

alter
The type of change to apply. Specify "move" to move the current cursor position or "extend" to extend the current selection.
direction
The direction in which to adjust the current selection. You can specify "forward" or "backward" to adjust in the appropriate direction based on the language at the selection point. If you want to adjust in a specific direction, you can specify "left" or "right".
granularity
The distance to adjust the current selection or cursor position. You can move by "character", "word", "sentence", "line", "paragraph", "lineboundary", "sentenceboundary", "paragraphboundary", or "documentboundary".
Note: Gecko does not implement "sentence", "paragraph", "sentenceboundary", "paragraphboundary", or "documentboundary". Webkit and Blink do.

Note: Starting in Gecko 5.0, the "word" granularity no longer includes the following space, regardless of the default platform behavior. This makes the behavior more consistent, as well as making it work the same way WebKit used to work, but unfortunately they have recently changed their behavior.

Example

To extend the current text selection to include the next word:

var selection = window.getSelection();
selection.modify("extend", "forward", "word");

Specifications

This method is not part of any specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
ExperimentalNon-standard
Chrome Full support YesEdge ? Firefox Full support 4IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
node parameter is nullable
Experimental
Chrome Full support YesEdge ? Firefox Full support 55IE ? Opera Full support YesSafari Full support YesWebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support 55Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
offset parameter is optional
Experimental
Chrome Full support YesEdge ? Firefox Full support 55IE ? Opera Full support YesSafari Full support YesWebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support 55Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.

See also

Document Tags and Contributors

Last updated by: ddbeck,