How to externally trigger d3 events
If you are already on D3 v4, you can use selection.dispatch()
which was specifically designed to do exactly what you are looking for:
# selection.dispatch(type[, parameters]) <>This was included in v4 as a result of the issue "Ability to trigger event handlers manually. #100".Dispatches a custom event of the specified type to each selected element, in order.
Furthermore, the method will enable you to dispatch events of the same type to all elements contained in the selection. The implementation of that method looks similar to the approach the other answerers took by putting event.dispatch()
to use, but will make your life somewhat easier. The following snippet has a listener for each individual circle, which may all be triggered by the button at once.
var circles = d3.select("svg").selectAll("circle")
.data(d3.range(5))
.enter().append("circle")
.attr("cx", function(d, i) { return 60 * i + 20; })
.attr("cy", "30")
.attr("r", "20").attr("fill", "blue")
.on("mouseleave",function(){
d3.select(this)
.attr("fill", "red")
.transition().duration(1000)
.attr("fill", "blue");
});
d3.select("#btn")
.on("click", function() {
circles.dispatch("mouseleave");
});
<script src="https://d3js.org/d3.v4.js"></script>
<svg width="400" height="70"></svg>
<button id="btn">Dispatch mouseleave to all circles</button>
programmatically access a d3 rectangle on click event
The on-click event function can be accessed by using the following:
testing.__onclick();
How to programmatically trigger a D3 drag event?
Save the callback (the one you pass into the drag handler) in a variable and then call this variable in your other context.
var dragCallback = function(){
console.log(d3.event.dx, d3.event.dy);
};
var dragBehavior = d3.behavior.drag()
.on("drag", dragCallback);
myNodes.enter()
.append("g")
.call(dragBehavior);
//Call drag method programmatically
dragCallback()
How can I simulate a click on d3 tree nodes?
You can simulate click event with following function:
function simulateClick(elem /* Must be the element, not d3 selection */) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(
"click", /* type */
true, /* canBubble */
true, /* cancelable */
window, /* view */
0, /* detail */
0, /* screenX */
0, /* screenY */
0, /* clientX */
0, /* clientY */
false, /* ctrlKey */
false, /* altKey */
false, /* shiftKey */
false, /* metaKey */
0, /* button */
null); /* relatedTarget */
elem.dispatchEvent(evt);
}
Demo @ JsFiddle: http://jsfiddle.net/ur5rx/1/Relevant Docs @ Mozilla Developer Network
- Creating and triggering events
- event.initMouseEvent
- EventTarget.dispatchEvent
D3js setting fill to none programmatically impacts onClick Event
You need to adjust the pointer-events property so that your unfilled shapes still react to mouse clicks. It sounds like
pointer-events: visible;
is probably what you want here.
Related Topics
Domnodeinserted Equivalent in Ie
Adding Csrftoken to Ajax Request
Event.Path Is Undefined Running in Firefox
How to Parseint a String with Leading 0
Execute an Exe File Using Node.Js
External Resource Not Being Loaded by Angularjs
Printing a Web Page Using Just Url and Without Opening New Window
Why Does This Foreach Return Undefined When Using a Return Statement
Check If Array Contains All Elements of Another Array
How to Add My Own Methods to HTMLelement Object
Read a Text File Using Node.Js
Sampling a Random Subset from an Array
Intercept Paste Event in JavaScript
Jquery:Select All Element with Custom Attribute
JavaScript .Replace Only Replaces First Match
Omitting the Second Expression When Using the If-Else Shorthand