javascript - How should I properly use setAttribute() method? -


i have single svg element i'm adding rect element createelement() method , giving width , height setattribute() method.

var svg = document.queryselector("svg");  function addrect(side) {    var newrect = document.createelement("rect");    svg.appendchild(newrect);    var thisrect = svg.lastchild;    thisrect.setattribute("width", side);   thisrect.setattribute("height", side); }  addrect("100"); 

http://codepen.io/stromqvist/pen/ywzpnb

the result in chrome dev tools show <rect width="100" height="100"></rect>, yet rect doesn't have dimensions.

what doing wrong?

when creating svg elements, you'd use createelementns create elements qualified namespace, svg elements

document.createelementns("http://www.w3.org/2000/svg", "svg"); 

and attributes you'd use setattributens, regular attributes width , height, setattribute should work

svg.setattributens("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink"); 

demo click here


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -