Dominofoe Dominofoe - 2 months ago 13
jQuery Question

Target the first child of all instances of a class using jquery

I have 2 buttons inside of a div.

<div class="pull-left address-selector">
<a class="btn btn-primary btn-sm"><i class="fa fa-envelope"></i></a>
<a class="btn btn-primary btn-sm"><i class="fa fa-wrench"></i></a>
</div>


The class
.address-selector
is found in many places on the page.
How do i select the first child of all the instances of
.address-selector
?

$(".address-selector a:first").hide();
This just works on the first found instance of
.address-selector

Answer

Use :first-child

While :first matches only a single element, the :first-child selector can match more than one: one for each parent. This is equivalent to :nth-child(1).

In this case:

$(".address-selector a:first-child").hide();