The getTargetRanges() method of the InputEvent interface returns an array of static ranges that will be affected by a change to the DOM if the input event is not canceled.

This allows web apps to override text edit behavior before the browser modifies the DOM tree, and provides more control over input events to improve performance.


var staticRanges[] = inputEvent.getTargetRanges()



Return value

An array of StaticRange objects.


Feature Detection

The following function returns true if beforeinput, and thus getTargetRanges, is supported.

function isBeforeInputEventAvailable() {
  return window.InputEvent && typeof InputEvent.prototype.getTargetRanges === "function";

Basic usage

The following example selects a contenteditable element and utilizes the beforeinput event to log the result of getTargetRanges().

const editableElem = document.querySelector('[contenteditable="true"]');

editableElem.addEventListener('beforeinput', (e) => {
    const targetRanges = e.getTargetRanges();


Specification Status Comment
Input Events Level 2
The definition of 'getTargetRanges()' in that specification.
Working Draft Initial definition.

Browser compatibility

BCD tables only load in the browser