I am trying to add a class to an SVGRect element that I created with JavaScript. I can see the class is added to the element (in Developer Tools) but the element is not styled accordingly. If I add a class to a Rect that was created inline the element is styled.
- The SVG element has Rect0
- I use "drawSomething" to add Rect1 to the SVG element
- I use "updateSomething" to add classes "task0" and "task1" to Rect0 and Rect1.
Rect0 is styled, Rect1 is not.
Do I need to "apply" after updating? The width of both Rect elements is updated after executing "updateSomething" which leads me to think that I don't need an "apply".
The "rect" rule is also not applied to the Rect created dynamically.
Also, I should also mention that my SVG element is part of an Angular component. Wondering if that changes anything.
function drawSomething()
{
let SVG_NS = "http://www.w3.org/2000/svg";
let svg = document.getElementById('editorSvg');
let rect = document.createElementNS(SVG_NS, "rect");
rect.setAttribute("id", "svg-rect-1");
rect.setAttribute("x", "100");
rect.setAttribute("y", "80");
rect.setAttribute("width", "50");
rect.setAttribute("height", "50");
svg.appendChild(rect);
}
function updateSomething()
{
let rect0 = document.getElementById("svg-rect-0");
rect0.style.width = "100";
rect0.setAttribute("class", "task0");
let rect1 = document.getElementById("svg-rect-1");
rect1.style.width = "100";
rect1.setAttribute("class", "task1");
}
drawSomething();
updateSomething();
rect {
stroke: red;
stroke-width: 2;
}
.task0 {
fill: green;
stroke-width: 5;
}
.task1 {
fill: orange;
stroke-width: 5;
}
<div id="svgContainer" class="svgContainer">
<svg id='editorSvg' class='editorSvg' xmlns="http://www.w3.org/2000/svg">
<rect id="svg-rect-0" x="10" y="10" width="50" height="50" class="task" ></rect>
</svg>
</div>