This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2023.
The focus() method of the MathMLElement interface sets focus on the specified MathML element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.
By default the browser will scroll the element into view after focusing it, and it may also provide visible indication of the focused element (typically by displaying a "focus ring" around the element). Parameter options are provided to disable the default scrolling and force visible indication on elements. If you call focus() from a mousedown event handler, you must call event.preventDefault() to keep the focus from leaving the MathMLElement.
focus() focus(options)
options OptionalAn object for controlling aspects of the focusing process. This object may contain the following properties:
preventScroll OptionalA boolean value indicating whether or not the browser should scroll the document to bring the newly-focused element into view. A value of false for preventScroll (the default) means that the browser will scroll the element into view after focusing it. If preventScroll is set to true, no scrolling will occur.
None (undefined).
This example uses a button to set the focus on a MathML circle element.
<div>
<math>
<msup id="myMath" tabindex="0">
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
<button id="focusButton">Focus the Math</button>
</div>
const mathElement = document.getElementById("myMath");
document.getElementById("focusButton").addEventListener("click", () => {
mathElement.focus();
});
| Specification |
|---|
| HTML> # dom-focus-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
focus |
109 | 109 | 71 | 95 | 13.1 | 109 | 79 | 74 | 13.4 | 21.0 | 109 | 13.4 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MathMLElement/focus