I am new to Javascript and SVG. Hope you guys can help me. I created a function addEltToSVG()
function addEltToSVG(svg, name, attrs) {
var element = document.createElementNS("http://www.w3.org/2000/svg", name);
if (attrs === undefined) attrs = {};
for (var key in attrs) {
element.setAttributeNS(null, key, attrs[key]); // namespace = null, name = key, value = attrs[key]
}
if (name == "text") {
var textNode = document.createTextNode(attrs['content']);
element.appendChild(textNode);
}
svg.appendChild(element); // insert elements to the svg
}
Then, I used this function like this. And it doesn't show up anything. I searched for solutions but it still doesn't work.
var width = 600, height = 400;
var s = document.createElementNS("http://www.w3.org/2000/svg", 'svg'), chartID = "histogram";
s.setAttribute("width", width);
s.setAttribute("height", height);
var rect1 = addEltToSVG(s, "rect", {
"width": 50,
"height": 10,
"fill": "red"
});