-2

The following code will copy ONE attribute: 'fill-rule' from svg(external svg) to snap paper(internal svg). How can I copy ALL the attributes and syling from one svg to another svg please? Any ideas would be greatly appreciated.

s1.attr("fill-rule", svg.node.getAttribute("fill-rule"));

Edit: If I use Roberts code, why this doesn't copy the attrs from svg to s1?:

s1.attr(attrs.item(i).svg);

live: http://jsbin.com/geyog/5/edit

  • Because s1.attr takes two arguments to set a value, you're using it as a getter. – Robert Longson Jul 30 '14 at 07:59
  • @ Robert. Do you mean something like: s1.attr("*", attrs.item(i).svg); – user3117671 Jul 30 '14 at 08:33
  • No I don't. You need to research the arguments attr takes. – Robert Longson Jul 30 '14 at 08:38
  • @ Robert. Here I found this info: Arguments name (DOMString) required The name of the attribute to create or alter. value (DOMString) required The string value for the attribute. But in this case I don't know the name of the attr to be created. So what should I do? – user3117671 Jul 30 '14 at 08:50
  • The name of the attribute is the nodeName property I provided in my answer. If you do some research you might find the other property you need too. – Robert Longson Jul 30 '14 at 08:52

1 Answers1

1

there's an attributes property in the DOM

if you had

<svg id="svg" preserveAspectRatio="none" width="100%"/>

you can do this...

var svg = document.getElementById("svg");
for (var i=0, attrs=svg.attributes, l=attrs.length; i<l; i++){
    s1.attr(attrs.item(i).nodeName, attrs.item(i).value);
}

presumably in your case it would be

var svg = svg.node;
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • Thank you very much @ Robert.How can I copy those attributes to s1 instead of alerting them please ?: http://jsbin.com/geyog/4/edit – user3117671 Jul 29 '14 at 12:03
  • @ Robert.I did some research and found that I need to have a pair of a nodename and a value so I need to add: alert(attrs.item(i).value; to get the values for those node names but still cannot find a way to append those into the snap paer, s1. Could you please help with that?! – user3117671 Aug 02 '14 at 08:16
  • Thank you Robert. Last night I was playing with it after I asked you and then figured out your updated answer after finding the difference between snap attr and js attribute. Thanks for making me do it by myself and learn! – user3117671 Aug 02 '14 at 20:14