score:29
The selector needs to be circle:nth-child(3)
-- the child
means that the element is the nth child, not to select the nth child of the element (see here).
You could also use:
// JS
var data=[ 1,2,3,4,5,6,7,8,9 ];
var svg = d3.select("body").append("svg");
var circles = svg.append("g").attr("id", "groupOfCircles")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d){ return d*20;})
.attr("cy", function(d){ return d*10;})
.attr("r" , function(d){ return d;})
.attr("fill","green");
d3.select("circle:nth-child(3)").attr("fill","red"); // <== CSS selector (DOM)
d3.select(circles[0][4]).attr("fill","blue"); // <== D3 selector (node)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
score:0
Here is another option by using a function as the selector.
circles.select(function (d,i) { return (i==3) ? this : null;})
.attr("fill", "red");
If the selector is a function it gets the datum (d) and the iterator (i) as parameter. It then returns either the object (this) if selected or null if not selected.
score:1
If you want to do it in d3 logic, the anonymous function always has an index parameter aside the data:
my_selection.attr("fill",function(d,i) {return i%3==0?"red":"green";});
score:3
You can also use your circles array to set the element's attribute:
d3.select(circles[3]).attr("fill","red");
// JS
var data=[ 1,2,3,4,5,6,7,8,9 ];
var svg = d3.select("body").append("svg");
var circles = svg.append("g").attr("id", "groupOfCircles")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d){ return d*20;})
.attr("cy", function(d){ return d*10;})
.attr("r" , function(d){ return d;})
.attr("fill","green");
var group = document.querySelector('#groupOfCircles');
var circleNodes = group.getElementsByTagName('circle');
d3.select(circleNodes[3]).attr("fill", "red");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
score:8
d3 v4 now supports using selection.nodes() to return an array of all elements of this selection. Then, we can change the attributes of nth element by d3.select(selection.nodes()[n]).attr(something)
// JS
var data=[ 1,2,3,4,5,6,7,8,9 ];
var svg = d3.select("body").append("svg");
var circles = svg.append("g").attr("id", "groupOfCircles")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d){ return d*20;})
.attr("cy", function(d){ return d*10;})
.attr("r" , function(d){ return d;})
.attr("fill","green");
circleElements = circles.nodes(); // <== Get all circle elements
d3.select(circleElements[6]).attr("fill","red");
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- d3js : How to select nth element of a group?
- How to select a specific d3 node group element
- d3js how to select the element i'm currently working on
- D3- how to select circles in a group element and set display to none
- How to select parent element of current element in d3.js
- How can I drag group (g) element in D3.js?
- How to replace a d3js nest function with group and rollup
- D3 how to select element by ID when there is a dot in ID
- how to give href to d3js text element
- How to assign click event to every svg element in d3js
- d3.js - how to delete a group element
- How come that D3.js can select an element that jQuery can't?
- Appending element clone to svg group in d3js
- D3 node update - how to select a specific text element among multiple ones?
- how to select svg element by variable name for id
- D3 - how to select recently appended element
- D3: How to select every thing under svg element
- Select the class of an element on mouseover D3js
- How to select an element by a variable id in d3?
- How to append `text` element by conditional in d3js
- How to Select Element by Class and Retrieve its ID in D3
- How to select all types of child element inside an SVG using d3.js
- Select content of text element with tspan specifier in SVG with D3js
- How to display array element on click event with D3JS
- trouble with D3js world map and svg group element width
- how can i group JSON data and generate a graph of each group in D3js
- How to select an element in svg in D3
- How to use d3.js to select an svg element and modify its attr?
- How can I append an element after select in d3?
- How to select multiple svg elements using d3js
More Query from same tag
- How to repeat rotation using d3
- d3.format thousand separator on variables?
- Creating DC charts dynamically
- Pass variable from Express to Client JavaScript
- How do you create a d3 line function that will draw 2 lines for x, y1, y2 data?
- D3 Selecting all elements having a certain class or combination of classes
- converting a d3.csv method to d3.csv.parse method
- How to create more than one d3 polygon
- Remove a svg element after transition in d3
- Convert SVG to PNG and maintain CSS integrity
- d3 js tree layout multiple parents
- D3 axis lable scale
- D3.js xy linechart with ordinal scale
- Converting string time into milliseconds
- D3 v4 Scaling Data to Range
- d3.js v4 wacky link transition in collapsible tree example
- D3 - Making stroke-width covary with second data set
- D3 Mathematical Function Create
- TypeError: undefined is not an object (evaluating 'M.layout.force')
- Modify d3.js code to generate static 3d plot
- Getting d3.js to label "00 02 04..." as "12AM 2AM 4AM..." in d3.scale.linear()
- Wait for d3 to load
- Conditionally change opacity of all nodes and edges recursively (d3)
- d3: setting classes for nodes doesnt work
- Why does this vanilla js function return different results in d3v3 and d3v4
- Can't display a single bar in D3
- Trying to make d3js transitions shrink or grow to the next value
- D3 nest an image and a circle for a node?
- Flatten D3.js Nested Data or Map it to new Dataset
- Delay start of force simulation