user2894296
user2894296

Reputation: 630

D3 how to select text elements given a specific id attribute

I need to select svg text elements that have the 'id' attribute value 's'.

Tried like below but it doesn't help..

d3.selectAll("[id=s]").selectAll("text");

Upvotes: 1

Views: 5600

Answers (4)

user2894296
user2894296

Reputation: 630

Was able to get this done through this.

d3.selectAll("[id=" + id + "]").filter(".someClass");

Upvotes: -1

Gilsha
Gilsha

Reputation: 14591

The id global attribute defines a unique identifier (ID) which must be unique in the whole document. So you cannot have multiple text elements with the same id.

To select an element by id and selecting all the text elements within that element :-

var texts = d3.select("#id").selectAll("text");

To select all text element having an attribute value :-

var texts = d3.selectAll("text[attrName=attrValue]");

Upvotes: 1

korakot
korakot

Reputation: 40828

Maybe, you want to use 'class' instead of id

d3.selectAll(".s")

Normally, ID is unique to each element.

Upvotes: 3

Robert Longson
Robert Longson

Reputation: 124059

You're only allowed one element with an id of s per document so

d3.select("#s")

would select it no matter what it is.

Upvotes: 1

Related Questions