rekoDolph
rekoDolph

Reputation: 823

How do I select only nodes with a certain CSS class ? D3/JS

I want to select only nodes with a "hidden" class applied to it. I have other shapes that have the "hidden" class applied but I only need to select the nodes.

I thought this may work :

var nodesVisible = inner.selectAll(".node").selectAll(".hidden")
            .classed("hidden", false);

In the HTML it has : class: "node hidden"

So I have also tried

var nodesVisible = inner.selectAll("node hidden")

But this doesn't select the nodes with the hidden class applied

Any ideas ?

Upvotes: 0

Views: 167

Answers (2)

moroccan_dev
moroccan_dev

Reputation: 310

you can do it only using pure javascript like this

HTML

<div id="c" class="hidden otherclass">  </div>

JAVASCRIPT

var div=c //get the element by id

if(div.className.indexOf('hidden')>-1&&
   div.className.indexOf('otherclass')>-1){

alert("yes the element has a hidden and otherclass classes")
// do other stuff
} 

UPDATE

i added >-1 in condition

Upvotes: 1

Vincent Beltman
Vincent Beltman

Reputation: 2114

Classes need to prepended with a dot:

var nodesVisible = inner.selectAll(".node.hidden")

Select all uses css selectors. Here is the doc about css selectors: http://www.w3schools.com/cssref/css_selectors.asp

Upvotes: 1

Related Questions