SachiDangalla SachiDangalla - 1 year ago 81
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

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download