This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The getElementById() method of the SVGSVGElement interface searches the SVG document fragment (i.e., the search is restricted to a subset of the document tree) for an Element whose id property matches the specified string.
getElementById(id)
idThe ID of the element to locate. The ID is a case-sensitive string which is unique within the SVG document fragment; only one element should have any given ID.
An Element object describing the DOM element object matching the specified ID, or null if no matching element was found in the SVG document fragment.
<svg id="exampleSVG" width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <circle id="circle1" cx="100" cy="100" r="50" fill="blue" /> </svg> <button id="getElementButton">Get Element</button> <p id="elementDisplay"></p>
const svgElement = document.getElementById("exampleSVG");
const getElementButton = document.getElementById("getElementButton");
const elementDisplay = document.getElementById("elementDisplay");
getElementButton.addEventListener("click", () => {
const circleElement = svgElement.getElementById("circle1");
if (circleElement) {
elementDisplay.textContent = `Element found: ${circleElement.tagName}`;
} else {
elementDisplay.textContent = "Element not found.";
}
});
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
getElementById |
7 | 12 | 1.5 | ≤12.1 | 5.1 | 18 | 4 | ≤12.1 | 5 | 1.0 | 3 | 5 |
© 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/SVGSVGElement/getElementById