Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first child in AngularJS directive

I want to get and modify the first child of the given element in an Angular directive.

<div class="wanted">
</div>
<div class="not-wanted">
</div>
<div class="not-wanted">
</div>

I already tried

elm.children('.wanted')

but then my directive modifies all children. How can I do this correctly?

like image 288
roschulze Avatar asked Jun 22 '15 07:06

roschulze


2 Answers

You can either use:

elm.children().first();

or:

elm.children(':first')

Both will have the same effect. The second one is slightly more performant.

like image 158
Tomek Sułkowski Avatar answered Oct 06 '22 03:10

Tomek Sułkowski


I use: element.children().eq(0)

children() - Get the children of each element in the set of matched elements

eq() - Reduce the set of matched elements to the one at the specified index

like image 24
Peter Hedberg Avatar answered Oct 06 '22 04:10

Peter Hedberg