Baseline Widely available
The getTargetRanges()
method of the InputEvent
interface returns an array of StaticRange
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 of inputType
and the current editing host, the expected return value of this method varies:
getTargetRanges()
"historyUndo"
or "historyRedo"
Any empty Array All remaining contenteditable
an Array of StaticRange
objects associated with event All remaining input
or textarea
an empty Array Syntax Parameters
None.
Return valueAn array of StaticRange
objects.
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();
console.log(targetRanges);
});
Specifications Browser compatibility
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4