This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
The SVGElement.focus() method sets focus on the specified SVG 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.
focus() focus(options)
options OptionalAn optional 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 an SVG circle element.
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"> <circle id="myCircle" cx="100" cy="100" r="50" tabindex="0" fill="blue" /> <button id="focusButton">Focus the circle</button> </svg>
document.getElementById("focusButton").addEventListener("click", () => {
const circle = document.getElementById("myCircle");
circle.focus();
});
| Specification |
|---|
| HTML> # dom-focus-dev> |
SVGElement.focus() from a mousedown event handler, you must call event.preventDefault() to keep the focus from leaving the SVGElement
| 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 |
1 | 17 | 51 | 15 | 3 | 18 | 51 | 14 | 1 | 1.0 | 4.4 | 1 |
options_preventScroll_parameter |
78 | 17 | 68 | 65 | 15 | 78 | 68 | 56 | 15.5 | 12.0 | 78 | 15.5 |
SVGElement.blur to remove the focus from an element.HTMLElement.focus a similar method for HTML elements.
© 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/SVGElement/focus