klewis
klewis

Reputation: 8350

How to find first instance of a specific element at first child level

If I have the following code...

<div id="wrapper">
    <h5>
        <div>something</div>
    </h5>
    <div>more</div>
</div>

...what is the easiest way to tell jquery, find the first div element at the first child level, which would be <div>more</div>, so that I can place a border around it?

I tried playing around with the following...

$('#wrapper').next('div').css({'border':'solid 2px pink'});

but no luck.

FYI - I know that a div is not suppose to sit inside an h5 element, that is out of my control to modify.

Upvotes: 0

Views: 469

Answers (2)

rrk
rrk

Reputation: 15846

jQuery has children() and first() which can help you with this.

$('#wrapper').children('div').first().css({'border':'solid 2px pink'});

Upvotes: 0

Chitrang
Chitrang

Reputation: 2114

You can use jQuery child selector and then :first selector from that.

$('#wrapper>div:first').css({'border':'solid 2px pink'});

Upvotes: 2

Related Questions