<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>SVGSVGElement: getElementById() method / notebook
SVGSVGElement: getElementById() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
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.";
}
});