Jeroen Bellemans
Jeroen Bellemans

Reputation: 2035

How can I target the child of the second element jQuery

How can I target the child of the second element with jQuery:

<div class="item active"></div>
<div class="item active">
    <div class="overlay"></div>
</div>
<div class="item active"></div>
<div class="item active"></div>

I tried these:

$(".item .active:nth-child(2) .overlay").addClass("test");
$(".item .active:nth-child(2)").find("overlay").addClass("test");

But none worked.

What did I do wrong, how can I target it?

Upvotes: 1

Views: 32

Answers (1)

miladfm
miladfm

Reputation: 1526

just remove space between .item and .active

$(".item.active:nth-child(2) .overlay").addClass("test");
$(".item.active:nth-child(2)").find("overlay").addClass("test");

then both of them work fine

see my code : https://codepen.io/miladfm/pen/jwBpZo

Upvotes: 1

Related Questions