Nick
Nick

Reputation: 936

Get child of array item

Hopefully the following 2 lines explains clearly what I am trying to achieve. Error returned is "items[1].find is not a function".

I can see the issue, as items[1] is not an object, but I can't work out how to achieve the following logic:

var items = $('.items'); // array
item_child = items[1].find('.child-class');

Upvotes: 0

Views: 28

Answers (1)

Neil
Neil

Reputation: 14321

One reason you could be getting the error is if there isn't two objects with $(".item"). Arrays and .eq() both start counting at 0. So:

<div class="items"> <!-- Index: 0 -->
    <div class="child-class">
        c1
    </div>
    <div class="child-class">
        c2
    </div>
</div>

<div class="items"> <!-- Index: 1 -->
    <div class="child-class">
        c1
    </div>
    <div class="child-class">
        c2
    </div>
</div>

Secondly, this isn't the error your currently getting, but it will cause one. items[0] returns a JavaScript object, whereas items.eq(1) returns a jQuery object, that you can then use jQuery functions with, like find.

var items = $('.items'); // array
item_child = items.eq(1).find('.child-class');

Upvotes: 1

Related Questions