score:3
You have to get the class and subtasks attributes as shown in below code inside the click listener.
d3.select(this).attr("class");
d3.select(this).attr("subtasks");
You may also use this.className
to get the class attribute. But since subtasks
is a custom attribute, you will have to get it use the d3 attr
method or html getAttribute
method.
To pass the attributes to some other functions from click listener, try any of the methods shown below.
The parameter d gives the data object binded to the corresponding element.
.on("click", function(d) {
expandTasks(d[7],d[3],d[4]);
});
or
.on("click", function(d) {
var id = d3.select(this).attr("id"); // or this.id
var className = d3.select(this).attr("class");// or this.className
var subtasks = d3.select(this).attr("subtasks"); // or this.getAttribute("subtasks");
expandTasks(id,className,subtasks);
});
score:0
There is an issue in how you access the attributes of your rect
. As you want to have more flexibility in getting the attributes to another function, may I propose the following:
Replace your on
event with the following line:
.on("click", function() {expandTasks(d3.select(this)); });
And you expandTasks
method with this one:
function expandTasks(d) {
alert(d.attr("id") + '/' + d.attr("class") + '/' + d.attr("subtasks"));
};
Essentially you are passing the g
element to the expandTasks
method, and then you can access any parameter of the element using d.attr(****)
.
Hope this helps.
score:1
In addition to the other answers that solve your immediate problem, note that it could be worth it to remap your data prior to joining it to your selection. Something like
var rectangles = pad
.selectAll("rect")
.data(data.map(function(d) {
return {
id: d[7],
classname: d[3],
subtask: d[4]
};
}));
where your transform your (presumed) arrays into objects with understandable keys.
You could then rewrite your attributes assignments and your handler as
var projectRectangles = rectangles.append("rect")
.attr("id", function(d) { return d.id; })
.attr("class", function(d) { return d.classname; })
.on("click", function(d) {
expandTasks(d.id, d.classname, d.subtask);
});
And a demo
var data = [
[null, null, null, 'class1', 'subtasks1', null, null, 'id1'],
[null, null, null, 'class2', 'subtasks2', null, null, 'id2']
];
var expandTasks = function() {
alert(Array.prototype.join.call(arguments, ' '))
};
var pad = d3.select('body').append('svg').append("g");
var rectangles = pad.selectAll("rect")
.data(data.map(function(d) {
return {
id: d[7],
classname: d[3],
subtask: d[4]
};
}))
var projectRectangles = rectangles.enter().append("rect")
.attr('x',function(d, i) {return i*100;})
.attr('y',0)
.attr('width',100).attr('height',100)
.attr("class", function(d) { return d.classname; })
.on("click", function(d) {
expandTasks(d.id, d.classname, d.subtask);
});
svg {background-color: beige; width:200px; height:100px}
.class1 {fill: red}
.class2 {fill: green}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- How can I pass attributes of an SVG element to a new event in D3?
- How do you make an SVG element mouse event bubble up through another element?
- How can I convert SVG coordinates (x,y) defined relatively to an element into absolute coordinates/position?
- In d3.js, how can I check if an element has been removed without doing a new selection?
- How to assign click event to every svg element in d3js
- how to add dragend event on svg element
- d3 how to insert new SVG element before existing other element?
- How can I get the dom svg element from d3 node data?
- How to pass an event from one element to another in d3?
- How can I animate the top of my svg element like in this gif using d3.js?
- How can I set the background color of a svg element which contains a D3 heat map?
- Can svg element trigger click event of svg elements under itself?
- How can I remove drag event on one element in a draggable group?
- D3 How can i replace a SVG element
- D3 - How can I show/hide a text element when hovering a circle element created from different attributes of the same data entry?
- How can I toggle the value of an svg text element with D3.js when user clicks the text?
- How can I remove or replace SVG content?
- 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.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- SVG element loses event handlers if moved around the DOM
- how to assign unique id to SVG text element in d3 javascript
- Can I move an SVG element between SVG groups, in d3.js
- How can I dynamically resize an SVG rect based on text width?
- how can I exclude an element from an Angular scope?
- Can I append a literal SVG element with d3?
- programmatically trigger click event in svg rect element
- New to nvD3 - how can I make my unix timestamps appear as dates on the x-axis?
- How can I get "call" to execute for each data element in D3?
More Query from same tag
- How to place a label in an area outside of an SVG path?
- d3js tree.nodes() is not a function
- Line Plus Bar with Multi Bars?
- Pie chart animation when data changes in plottable.js
- D3 Zoomable Circle Packing in IE11 is expanding beyond the SVG boundary
- d3.js - extent width not match axis line with svg defs
- How to reduce Y-Axis scale values in d3 bar chart?
- Vertically align nvd3 legend
- Why does transition remove text from my labels?
- How to get different shades of colors for a single color?
- need to set back ground color for c3 sub chart or make it less transparent than main chart
- mouse over event on axis label d3.js javascript
- MNaN and NaN coordinates on a d3.js streamgraph
- D3: How do you highlight a range of dates on D3 a line chart?
- Panning/Dragging of nodes restricts the Viewport
- d3 chart - get x axis time of max value
- Plotting data from json file with different date format
- Drag multiple items inside another item in d3
- Round the edges of a closed path in d3 donut chart
- How to add text on top the heatmap to properly show value for each rectangle when using D3.js?
- D3 Line Graph with JSON Data in ISO 8601 Format
- How to center a List inside a D3 circle
- Jumping behavior when dragging groups of elements using d3.drag
- Y-axis ticks' labels visible partially
- force directed graph - define the color of a node, if the node is source and target at the same time
- Curve tension is weird in d3 v4
- D3.js Styling a highlighted Table
- x axis domain issue in D3 Line chart
- d3js maximum and minimum on x scale.
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()