files/en-us/web/api/mathmlelement/focus/index.md
{{APIRef("MathML")}}
The focus() method of the {{domxref("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 {{optional_inline}}
preventScroll {{optional_inline}}
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 ({{jsxref("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();
});
{{EmbedLiveSample("focus",100,100)}}
{{Specifications}}
{{Compat}}