score:2
Reason your event fires only for first bar and not the dynamic ones is because of the way you added your event listener.
Your way only puts events on elements already present on the page (they are in DOM structure). Any new elements will not have this event listener tied to them.
You can make a quick check for this by putting your code in function like
function setListeners() {
svg.select(".bars").selectAll("rect").on("mouseover", function() {
d3.select(this)
.transition()
.attr("fill", "red");
})
}
After you add any new bars on the screen, add this function call and see if it works for all elements. If this is indeed the case, you need to write your event listener in a way it works for all elements, including dynamically added ones. The way to do that is to set the event to some of the parent DOM nodes and then checking if you are hovering on exactly the thing you want the event to fire.
Example:
$(document).on(EVENT, SELECTOR, function(){
code;
});
This will put the event on the body and you can check then selector after it's triggered if you are over correct element. However it was a while since I worked with D3 and I'm not sure how D3, SVG and jQuery play together, last time I was doing it they had some troubles. In this case event should be mouseover, selector should be your rect bars and function should be what ever you want to run.
If everything else fails in case they won't cooperate, just use the function to set event listeners and call it every time after you dynamically add new elements.
Source: stackoverflow.com
Related Query
- selectAll("rect") selects all rectangles, but doesn't apply the mouseover function to some
- dc.js apply some, but not all chart selections to numberDisplay, while maintaining the interactions between charts in place
- How can I draw a continuous line using d3 without needing to click&drag the mouse, but rather just use the mouseover event?
- d3.js get selected nodes and apply color for all the selected nodes
- Mouseover effect on all but first row in table
- How do I remove all children elements from a node and then apply them again with different color and size?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- GetBoundingClientRect but relative to the entire document
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- apply several mouseover events to neighboring (connected) nodes
- Can I use images as the background rectangles for d3 treemaps?
- Could someone once and for all please explain Cannot call determinedVisibility() - never saw a connection for the pid
- d3.select(this) works on mouseover, but not on function called in mouseover
- D3, retrieve the x position of a <g> element and apply multiple transform
- How can I start with all the nodes collapsed in d3js?
- Does Vega-Lite provide all the features that Vega provides?
- How can I toggle the class of all elements in a selection?
- d3.js click and apply zoom and pan to distribute points located inside a targeted division to the triggered subdivisions
- How to import a d3 v4 / v5 module into a Node project but keep the D3 v3 namespace style (d3.)?
- d3.js foreach loop on all the paths contained in a svg
- d3.js How to make all the nodes collapsed in Collapsible indented Tree
- How to make all the nodes circle the center node?
- How to have d3 tickFormat of (day, time) but only show time if day is the same?
- d3 linechart - Show 0 on the y-axis without passing in all points?
- Is there a select query to grab all but my current selection?
- How can I keep a d3 mouseover open while my mouse is over the tooltip?
- d3.js v4, how do I have a line follow the mouse on hover, but also have a circle follow the path?
- Mouseover event on two charts at the same time d3.js
- d3js prevent forceSimulation from recalculating position all the time
- Select all the paths and parent nodes of a child node in tree layout
More Query from same tag
- Using CSV for data
- d3.selection.prototype throwing an error with typescript and angular 2
- How can I get the natural width and height of an svg image element?
- Arrow marker shaded in SVG d3
- How to have an interactive D3 chart read a json file instead of csv data?
- d3 I want that gridlines overflow will be hidden
- How to access key values in an object using d3.js?
- d3js multi-line graph to iterate multiple nested arrays with multiple nested objects
- d3.js v3 and broken zoomable treemap example
- D3 v4 - Get the value of the each tick present on an axis?
- How to align graph with respective rectangle node size in d3 js
- How can I select :last-child in d3.js?
- Use a static force layout to create a network in Svelte and d3
- Is it possible to make offset/shift in d3.js scales?
- D3.js scatterplot from nested object
- markmap: Insert the mindmap to the HTML
- D3: How to access an attribute of a previous item
- Colored text and line-breaks for D3 node labels
- What object type is the instance element parameter pass into the link function of an angular directive?
- Why the data can't be bind with JSON file?
- Create d3 linear color legend using d3 colors
- D3 adding unwanted extra line to my line chart
- d3.js linear scale returns NaN
- How do I set svg path style in js?
- How to load a csv file from static in django into javascript function
- Is there a way to parse times and be able to plot them directly using javascript and d3?
- change the color of chart d3
- Reload d3.js graph on node click
- trouble with d3.js - scaling a path shape
- Format the legend of my donut chart