HTMLElement: editContext property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The editContext property of the HTMLElement interface gets and sets an element's associated EditContext object.

The EditContext API can be used to build rich text editors on the web that support advanced text input experiences, such as Input Method Editor (IME) composition, emoji picker, or any other platform-specific editing-related UI surfaces.

Value

An EditContext object or null.

Possible elements

Setting the editContext property only works on certain types of elements:

If you try to set the editContext property on an element that is not one of the above, a NotSupportedError DOMException is thrown.

Element association

Setting the editContext property of an element to an EditContext instance associates that element with the EditContext instance.

The association is one-to-one:

  • An element can only be associated to one EditContext instance.
  • An EditContext instance can only be associated to one element.

If you try to associate an already associated EditContext instance to a different element, a DOMException is thrown.

If you try to associate an other EditContext instance to an element that's already associated, a DOMException is also thrown.

To check whether an element is associated with an EditContext instance already, use the EditContext.attachedElements() method.

Garbage collection

An EditContext instance will keep its associated element alive if it has other live references, even if the associated element is removed from the DOM.

If you want to make sure the element is garbage collected, clear the editContext property of the element.

Examples

Setting an element's editContext property

This example shows how to set the editContext property of a <canvas> element to a new EditContext instance in order to make the element editable.

html
<canvas id="editor-canvas"></canvas>
js
const canvas = document.getElementById("editor-canvas");
const editContext = new EditContext();
canvas.editContext = editContext;

Clearing an element's editContext property

This example shows how to clear the editContext property of an editable <canvas> element in order to safely remove the element from the DOM.

html
<canvas id="editor-canvas"></canvas>
js
// Create the EditContext and associate it with the canvas element.
const canvas = document.getElementById("editor-canvas");
const editContext = new EditContext();
canvas.editContext = editContext;

// Later, clear the editContext property, and remove the element.
canvas.editContext = null;
canvas.remove();

Specifications

Specification
EditContext API
# dom-htmlelement-editcontext

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
editContext
Experimental
Chrome – Full support
Chrome 121 (Release date: 2024-01-23)
footnote Full support
Edge – Full support
Edge 121 (Release date: 2024-01-25)
footnote Full support
Firefox – No support
Firefox
footnote
footnote See bug 1904161
Opera – Full support
Opera 107 (Release date: 2024-02-07)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 269922
Chrome Android – Full support
Chrome Android 121 (Release date: 2024-01-23)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote
footnote See bug 1904161
Opera Android – Full support
Opera Android 81 (Release date: 2024-03-14)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 269922
Samsung Internet – Full support
Samsung Internet 25 (Release date: 2024-04-24)
footnote Full support
WebView Android – Full support
WebView Android 121 (Release date: 2024-01-23)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 269922

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.

See also