Mussé Redi
Mussé Redi

Reputation: 945

Traversing the DOM tree with JavaScript

I'm trying to traverse the document DOM to obtain certain html elements, so I can invoke their eventListeners with JavaScript.

To this end, I know that the elements I'm looking for are contained in a certain html class (dc-chart). Hence I obtain the base elements by

x = document.getElementsByClassName("dc-chart")

enter image description here

Now, my goal is to, say, traverse the rect class elements inside the 7th element above:

enter image description here

How would I go about doing this?

x.[7].getElementById("stack _0")

doesn't seem to work in my Chrome webconsole; getElementById can't be chained to x[7].

Upvotes: 0

Views: 141

Answers (1)

Damian Peralta
Damian Peralta

Reputation: 1876

"stack _0" is not an id, they are two classes separated by a blank space: "stack" and "_0". And you should use getElementsByClassName() instead of getElementById()

Upvotes: 2

Related Questions