Is there any method to find element by snap id in Snap svg?

3.8k views Asked by At

In raphael there is a Paper.getById() method and it works well, but in Snap there is no such method and I can't find any analogue one.

I see that Snap sets for every element on the paper the unique id something like this pathSian06waj35 . But I can't find any information in docs how to use it to get an Element object by that id.

1

There are 1 answers

2
Ian On BEST ANSWER

You don't typically use that at all, as its Snaps own reference, you would be more likely to set your own id (or it would be in the SVG markup), then access it your own variable, or via a css selector, its id, class etc.

Eg

var rect = s.rect(10,10,100,100).attr({ id: "myRect" });

//to access via id attribute
console.log( s.select('#myRect') );
// or
console.log( Snap("#myRect") );

Note: In early versions of Snap the id attribute didn't work correct, so make sure you are using version 3 onwards I think.