I am using this code to insert svg into a div tag.
var container = document.getElementById("div_id");
var svg = document.createElement("svg");
svg.setAttribute("width",container.clientWidth);
svg.setAttribute("height",container.clientHeight);
container.appendChild(svg);
On checking using the developer tools in the browser, svg is present inside the div. But when I hover over the svg in the developer tools, it is showing "svg 0*0" i.e eventhough the width and height attributes are set as 500 and 400 I cannot view it in the page. I tried to insert a line into the svg, which again can be seen inside svg, but not visible in the browser. Need help.
Use
var svg = document.createElementNS("http://www.w3.org/2000/svg",'svg');
to create your SVG. Use it for elements as well.
var circle = document.createElementNS("http://www.w3.org/2000/svg",'circle');
The work just like regular elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With