Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. InputEvent
  4. getTargetRanges()

InputEvent: getTargetRanges() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2021⁩.

ThegetTargetRanges() method of theInputEvent interface returns an array ofStaticRange objects 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.

Depending on the value ofinputType and the current editing host, the expected return value of this method varies:

inputTypeEditing hostResponse ofgetTargetRanges()
"historyUndo" or"historyRedo"Anyempty Array
All remainingcontenteditable an Array ofStaticRange objects associated with event
All remaininginput ortextarea an empty Array

Syntax

js
getTargetRanges()

Parameters

None.

Return value

An array ofStaticRange objects.

Examples

Feature Detection

The following function returns true ifbeforeinput, and thusgetTargetRanges, is supported.

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

Basic usage

The following example selects acontenteditable element and utilizes thebeforeinputevent to log the result ofgetTargetRanges().

js
const editableElem = document.querySelector('[contenteditable="true"]');editableElem.addEventListener("beforeinput", (e) => {  const targetRanges = e.getTargetRanges();  console.log(targetRanges);});

Specifications

Specification
Input Events Level 2
# dom-inputevent-gettargetranges

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp