You can use selection.call()
to re-use behavior on multiple selections. Here, I've defined a colorable
function which adds a CSS rule and a click behavior to this
, then used selection.call(colorable)
to invoke it on two different selections. There are two things to note: first, the colorable
function is called with the selection as this
. Second, you don't lose the ability to access the selection's data in your setters.
https://d3js.org/d3.v3.min.js