score:1

Accepted answer

Selections are generally transient; you don't need to keep them around if you can just as easily reselect them from the document. So, selecting by id is a reasonable option.

If you don't want to give your elements unique ids (which is sometimes a pain when creating visualizations generically), then another option is to store a reference to the associated elements via the bound data. For example:

selection.each(function(d) { d.element = this; })

Now, assuming that the same data d is bound to another element, you can d3.select(d.element) to select the original element. You might choose a more specific name than "element" to make it clear which of the two (original, and decorative) elements you are referring to.

On the other hand, if you have different data on different elements, then you'll need a different way to link them together. If you don't want to use an id or another suitable selector, then a map of references is reasonable too.


Related Query

More Query from same tag