Sohaib Mohammed Sohaib Mohammed - 5 months ago 42
Javascript Question

How to use Snap.svg code multiple times?

this demo .

I want to make Icons using Snap.svg, so i want end user to repeating HTML to getting multiple icons.

HTML:

<svg class="box"></svg>
<svg class="box"></svg> <!-- not work -->
<svg class="box"></svg> <!-- not work -->
<svg class="box"></svg> <!-- not work -->


JS:

var box = Snap(".box");
box.rect(0,0,100,100).attr({fill:f00});

Answer

You need to iterate through each .box and instantiate Snap on each element, this is an example:

var boxes = [].slice.call(document.querySelectorAll(".box"));
boxes.forEach(function(box){
  var box = Snap(box);
  box.rect(0,0,100,100).attr({fill:"#f00"});
});

Working example here:

http://codepen.io/anon/pen/XKXpEY

Comments