Absolutely! Here’s a fully functional example of creating and manipulating SVG elements using the DOM API (not string concatenation or innerHTML). This example will:
Create an SVG canvas.
Add a circle and a rectangle.
Allow you to add more circles by clicking a button.
Demonstrate how to update SVG attributes programmatically.
You can copy and run this code as a standalone HTML file.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG DOM API Example</title>
<style>
body { font-family: sans-serif; }
#svgContainer { border: 1px solid #ccc; display: inline-block; margin-top: 10px; }
button { margin: 10px 5px; }
</style>
</head>
<body>
<h2>SVG DOM API Example</h2>
<button id="addCircleBtn">Add Random Circle</button>
<button id="changeRectColorBtn">Change Rectangle Color</button>
<div id="svgContainer"></div>
<script>
// SVG namespace
const SVG_NS = "http://www.w3.org/2000/svg";
// Create SVG element
const svg = document.createElementNS(SVG_NS, "svg");
svg.setAttribute("width", 400);
svg.setAttribute("height", 300);
svg.style.background = "#f9f9f9";
// Add SVG to the container
document.getElementById("svgContainer").appendChild(svg);
// Create a rectangle
const rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("x", 50);
rect.setAttribute("y", 50);
rect.setAttribute("width", 100);
rect.setAttribute("height", 60);
rect.setAttribute("fill", "#4CAF50");
rect.setAttribute("stroke", "#222");
rect.setAttribute("stroke-width", 2);
svg.appendChild(rect);
// Create a circle
const circle = document.createElementNS(SVG_NS, "circle");
circle.setAttribute("cx", 200);
circle.setAttribute("cy", 120);
circle.setAttribute("r", 40);
circle.setAttribute("fill", "#2196F3");
circle.setAttribute("stroke", "#222");
circle.setAttribute("stroke-width", 2);
svg.appendChild(circle);
// Add event to add random circles
document.getElementById("addCircleBtn").addEventListener("click", () => {
const newCircle = document.createElementNS(SVG_NS, "circle");
// Random position and color
const cx = Math.random() * 350 + 25;
const cy = Math.random() * 250 + 25;
const r = Math.random() * 20 + 10;
const color = `hsl(${Math.random() * 360}, 70%, 60%)`;
newCircle.setAttribute("cx", cx);
newCircle.setAttribute("cy", cy);
newCircle.setAttribute("r", r);
newCircle.setAttribute("fill", color);
newCircle.setAttribute("stroke", "#333");
newCircle.setAttribute("stroke-width", 1.5);
// Add a tooltip on hover
newCircle.addEventListener("mouseenter", function() {
this.setAttribute("stroke", "#FF9800");
this.setAttribute("stroke-width", 4);
});
newCircle.addEventListener("mouseleave", function() {
this.setAttribute("stroke", "#333");
this.setAttribute("stroke-width", 1.5);
});
svg.appendChild(newCircle);
});
// Add event to change rectangle color
document.getElementById("changeRectColorBtn").addEventListener("click", () => {
const color = `hsl(${Math.random() * 360}, 70%, 60%)`;
rect.setAttribute("fill", color);
});
</script>
</body>
</html>
How this works:
SVG elements are created with document.createElementNS(SVG_NS, "elementName").
Attributes are set with .setAttribute().
Event listeners are attached directly to SVG elements.
No innerHTML or string concatenation is used—everything is done via the DOM API.
This is the recommended way to work with SVG dynamically, as it’s safe, efficient, and gives you full control over the SVG elements.