SachiDangalla SachiDangalla - 2 months ago 16
Javascript Question

How to externally trigger d3 events

I have a d3 selection upon which I have defined event callbacks.

var obj ="#kk").on("mouseleave",function(){
console.log("mouse leave");

How can I trigger the event externally? Is there something like:

obj.mouseleave(); // actuall mouse leave function calling

If there is, and if I select the object without referring to
, will the trigger still work?

As in:

newObje.mouseleave(); //will this trigger the previously defined instructions


The following will trigger the mouseleave event on the elements via dispatchEvent().

  var event = document.createEvent('Event');
  event.initEvent('mouseleave', true, true);


Example: (I've added a button at the bottom to trigger it. The mouseleave event is attached to the circles)