ThomasReggi
ThomasReggi

Reputation: 59395

$(this) with child and pseudo selector

I have the selection below, is there any way to make this selection work with $(this)?

(select the second div child of .container)

$(".container div:eq(2)").width();

Upvotes: 0

Views: 190

Answers (3)

Pablo Ziliani
Pablo Ziliani

Reputation: 11

What's wrong with:

$(this).find("div:eq(2)").width()

or:

$("div:eq(2)", this).width()

?

BTW that returns the width of the third div descendant from this (which unlike div:nth-child(2) may or may not have two siblings before it, :eq() refers to the position in the matching pseudo-array, not in the DOM)

Upvotes: 0

rahul
rahul

Reputation: 187060

Try

$(".container").each(function(){
    $(this).find("div:eq(2)"); // if its not a direct child
});

Upvotes: 1

matthewpavkov
matthewpavkov

Reputation: 2928

What you have there already is fine. Why do you want to use $(this)? You would use $(this) as an example, in a function:

$(".container div:eq(2)").click(function() {
    if( $(this).width > 100 ) {
        ...
    }
});

Upvotes: 0

Related Questions