javascript - programmatically access a d3 rectangle on click event -
this question has answer here:
i create few d3 rectangles , give them each click event: first created container:
var svgcontainer = d3.select("#container") .append("svg") .attr("width", 720) .attr("height", 45);
then in for-loop create each rectangle inside container:
var rectangle = svgcontainer.append("rect") .attr("x", 10 + xappend) .attr("y", 5) .attr("width", 120) .attr("height", 35) .attr("stroke", "black") .attr("name", name) .on("click", function () { // click event... //... });
now in different function, want trigger on-click function. variable 'rectangle' not global, gets created inside loop , create 6 rectangles loop.
what have done far inside other function is:
var tempcontainer = d3.select("#container"); var test = tempcontainer.selectall("rect");
this gives me array of 1 object , object array of 6 rectangles, loop through array find rectangle want.
for (var = 0; < test[0].length; i++) { if (search.item.desc == test[0][i].attributes[5].value) { var testing = test[0][i]; //testing.attributes[5].value works fine //testing.click(); not work //testing.on('click')(); not work } }
inside for-loop variable 'testing' rectangle want, can access attributes did in if statement above. cannot access , trigger on click function.
the on-click event function can accessed using following:
testing.__onclick();
Comments
Post a Comment