d3 icon indicating copy to clipboard operation
d3 copied to clipboard

d3.selectAll returns an empty array, but d3.select returns an element

Open kilometers opened this issue 12 years ago • 3 comments

I have elements with the class "state". When I use the code:

d3.selectAll(".state")

An empty array is returned, even though I've checked the page's DOM and there are several objects with "state" as their class. However, when I try it with d3.select, as so:

d3.select(".state")

An element is returned -- I'm at a bit of a loss here. I have tried using id's instead, but the behavior persisted. Surely I'm missing something?

kilometers avatar Apr 05 '14 02:04 kilometers

It returns an element, but that element is null. So, the effect is the same. Read more about select vs. selectAll in How Selections Work.

mbostock avatar Apr 05 '14 02:04 mbostock

Ah ha! So it does. Alright, time for some reading.

kilometers avatar Apr 05 '14 02:04 kilometers

I just figured out exactly what was amiss. My data is loaded via JSON, but the selectAll functions in question were being called outside of the d3.json callback. Derr. Slowly but surely.

kilometers avatar Apr 05 '14 02:04 kilometers