TubbyStubby TubbyStubby - 1 year ago 56
HTML Question

How can I get a particular element with JavaScript if there are many with same class?

I have this code. How can I get


<div id="div1">
<div class="a"></div>
<div class="b"></div>
<div id="div2">
<div class="a"></div>
<div class="b"></div>

Thanks for help. Just started learning JavaScript.

Answer Source

This should do it:

document.querySelector('#div1 > .a');

Together, this specifically selects the <div class="a"> element that is a child of <div id="div1">.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download