views:

29

answers:

1

I have a particular element (input box) that's deeply nested in some table stuff (td,tr, table). How can I select the "fieldset" that encompasses it all?

+1  A: 

You can do this, using .closest()

$("#myInput").closest("fieldset");
Nick Craver
One difference though is that closest starts its search at the object it's invoked on, instead of at the immediate parent. If #myInput was itself a fieldset, your examples would return different results, no?
Mike Sherov
@Mike - Yes in that case it's true, but I've never run into a place where that's been an issue...but always safer to use `.closest()` for sure. `:eq(0)` was the best alternative pre-1.3 I believe, been a while since I've have to use it for anything really. I didn't think you could include a `<fieldset>` directly beneath another, but double checking I see that it is valid...removing the `parents()` case since it can indeed be an issue here. Good catch Mike, didn't think it applied at all, but spec says it matters.
Nick Craver
Actually I was thinking more in the general case of a div inside a div. I think closest is a great tool but I just wanted to raise the important distinction... Of course, you could always just do $(elem).parent().closest()
Mike Sherov