0

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();
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

3 Answers3

1

Try

$(".container").each(function(){
    $(this).find("div:eq(2)"); // if its not a direct child
});
rahul
  • 184,426
  • 49
  • 232
  • 263
0

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 ) {
        ...
    }
});
matthewpavkov
  • 2,918
  • 4
  • 21
  • 37
  • I am piecing together a "plugin" that involves selecting $(".container div:eq(2)").width(), trying to think of a way to pass in the outermost div as a variable and I need to use $(this). – ThomasReggi Dec 10 '10 at 05:12
  • 1
    What about using `$(this).parent()`? – matthewpavkov Dec 10 '10 at 05:13
  • :eq(2) is almost the same to :nth-child(3) why are you suggesting going the opposite way? – ThomasReggi Dec 10 '10 at 05:16
  • @Thomas it depends which click hander you attached, if you attach parent if we can traverser forward ,if you attach child you will traverse opposite direction... – kobe Dec 10 '10 at 05:43
0

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)