svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
11.14k stars 1.08k forks source link

ID for element is undefined #1087

Closed pzhaojohnson closed 4 years ago

pzhaojohnson commented 4 years ago

ID for element is undefined

According to a previous thread, every element should be created with a unique ID, but it seems like an element is not given an ID until the id method is called:

let text = svg.text('asdf');
text.attr('id'); // gives undefined
text.id(); // gives an actual ID
text.attr('id'); // now gives the same ID as text.id()

Is this the intended behavior?

Thanks!

Fuzzyma commented 4 years ago

Yes it is. The thread you found is 6 years old. Issues were raised that all this ids are cluttering the svg and I agree. So we only create an id if needed. That shouldnt be a problem since you either set the id or it is created while getting it. Just dont use attr() to access the id or make sure that you call id after construction of elements