How to use css selector on SVGjs

319 Views Asked by At

I try multiple javascript libraries to work with svg

now i play with svgjs.dev

but i don't know how to use css selector like snapsvg

var result1 = Snap.select('#id_select');
var result2 = Snap.select('.class_selector');

what is the solution for svgjs ?

i can select childrens but i need make a loop to compare properties for every child :(

what is the shortcut fot get some element inner svg document?

thanks for your attention

2

There are 2 best solutions below

0
On BEST ANSWER

You would use the get() method as per doc for id, or use jquery if needing a class

var element = SVG.get('my_element')
element.fill('#f06')
1
On

From SVG.js v2 it is possible to use selectors. It's what you would expect it to be:

var elements = SVG.select('rect.my-class').fill('#f06')

You can also search within a parent element:

var elements = group.select('rect.my-class').fill('#f06')

But as @Ian suggested, jQuery or Zepto are options as well. This is all described in the docs:

https://svgdotjs.github.io/referencing/#using-css-selectors