Dominofoe
Dominofoe

Reputation: 603

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

Upvotes: 0

Views: 27

Answers (1)

fdomn-m
fdomn-m

Reputation: 28611

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();

Upvotes: 2

Related Questions