score:8
for (var index in nodeValues) {
var rect = svg.append("rect")
.attr(elementAttr(index))
.style("fill", "red" )
.on('click', function(d,i) {
// handle events here
// d - datum
// i - identifier or index
// this - the `<rect>` that was clicked
});
}
score:2
I gave a vote to Stephen's answer but it's actually just missing one part - instead of .click, it's
.on("click", function(d){...
Here's some documentation. Also, I've jacked Mike's example of zooming with circles in order to implement the binder listener. Here's a fiddle that shows it.
Hope that helps!
score:3
I've refactored your code to be more D3-like -- in particular, you don't need a loop if you use D3's selections and data matching. The code then looks like this:
svg.selectAll("rect")
.data(nodeValues).enter().append("rect")
.each(function(d) {
var attrs = elementAttr(d);
d3.select(this).attr(attrs);
})
.style("fill", rectColor);
Adding the click handler is just an additional statement at the end of this:
.on("click", function() {
d3.select(this).attr("width", 120);
});
Complete demo here.
Source: stackoverflow.com
Related Query
- How to assign click event to every svg element in d3js
- How to display array element on click event with D3JS
- how to assign unique id to SVG text element in d3 javascript
- How do you make an SVG element mouse event bubble up through another element?
- programmatically trigger click event in svg rect element
- How can I pass attributes of an SVG element to a new event in D3?
- how to add dragend event on svg element
- Get click event from SVG bounding box using d3js
- D3: How to select every thing under svg element
- Triggering a Click Event on an SVG element
- Can svg element trigger click event of svg elements under itself?
- How do append a <text> for every element of the data in a D3 svg
- how i get height of a svg element using d3js
- get specific word on click event for SVG Text element
- How to access the DOM element that correlates to a D3 SVG object?
- How can I make double click event on node in d3.js?
- How do I get the width of an svg element using d3js?
- d3 click coordinates are relative to page not svg - how to translate them (Chrome error)
- How to add a click event on nvd3.js graph
- d3js : How to select nth element of a group?
- SVG element loses event handlers if moved around the DOM
- D3js : How to convert svg text to path?
- How to use D3js on server side to generate SVG directly?
- Do I add eventListener to every SVG element if I follow normal D3 way?
- how to access data of a d3 SVG Element
- dagre-d3 how to click node and run an event after that
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- how to get svg element type
- Accessing D3 SVG object within on click event
- How can I convert SVG coordinates (x,y) defined relatively to an element into absolute coordinates/position?
More Query from same tag
- d3js - How to move x- domain values towards right
- Why does the NVD3 StreamGraph shift the y zero axis over time?
- Binding data to existing number of elements based on object property
- dc.js composite chart errors
- Increasing the Pseudo-Area of D3 Path Link For Easier D3-Tip Event Trigger
- Nested JSON not working in d3.js
- Prevent node overlap in force directed graph
- Loading csv data with d3.csv in nvd3 multiBar Chart example (JSON format)
- Defining CSS style for tooltip in d3.js body
- Change the Y axis range based on the selected data
- How to get the height and width of text in a span not the whole span width and height
- D3 v4 Pie Chart Update empty `d` attributes
- Zooming and Panning a Mercator Map centered on the Pacific using d3.js
- Selecting null: what is the reason behind selectAll(null) in D3?
- d3js replica of nested line graph
- d3.js transition().remove() not working
- How to create a d3 radial-tree from a list of nodes and links?
- d3 transition is not a function
- NVD3 - Using JSON url data with LinePlusBarChart (mapping values)
- How to make automatic type inference with adding totals & sorting
- Create D3 Bar Chart from array of timestamps
- d3 gauge chart with labels and percentages?
- d3js graph retaining old axis data on refreshing with new data
- d3/cola programmatic node drag using initMouseEvent
- Not able to parse Date in unix time format
- ASP.NET vs creating/opening .HTML file in C#
- Iteratively appending links to a D3 force directed network visualization
- Zoom with Axis: undefined is not an object (evaluating 'd3.event.transform')
- My D3.js code cause new data to be added as a new graph, I want it to be appended to an existing graph. What did I do wrong?
- show data of each circle of bubble chart in table d3.js