Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the second match with QuerySelector?

The following statement gives me the first element with the class titanic

element = document.querySelector('.titanic'); 

How would I retrieve the second element with the same class?

like image 364
Nicky Smits Avatar asked Apr 07 '14 02:04

Nicky Smits


People also ask

How do I select the nth-child in querySelector?

To get the nth-child of an element using the querySelector method, pass the :nth-child() pseudo-class as a parameter to the method, e.g. document. querySelector('#parent :nth-child(1)') . The nth-child pseudo-class returns the element that matches the specified position.

Can you select multiple elements with querySelector?

Yes, because querySelectorAll accepts full CSS selectors, and CSS has the concept of selector groups, which lets you specify more than one unrelated selector.

What does the querySelectorAll Method 2?

Definition and Usage The querySelectorAll() method returns all elements that matches a CSS selector(s). The querySelectorAll() method returns a NodeList.


1 Answers

Use document.querySelectorAll

document.querySelectorAll('.titanic')[1] 
like image 189
Phil Avatar answered Oct 04 '22 16:10

Phil