views:

967

answers:

3

I am struggling with how to select a particular element with jQuery. Conditions outlined here:

  • At the relevant point in my function, this references a span.
  • That span may be a child, grand child, or great grand child, but somewhere up the hierarchy, it's contained by a td.
  • I'd like to select the last span inside of the td.
  • The span may be this, or it may be another span.
  • The span tags to be selected is a direct child of the containing td, so last-child is workable. However, for future flexibility a solution that doesn't assume it's a direct child is preferred.

So far, I am using this to select the correct parent element:

$(this).parents('td')

I don't know how to traverse back down the DOM tree to get the final span inside of the selected td. Thanks!

Edit

Per my comments below, would also be nice to select from a span or div, whichever is last.

+4  A: 
$(this).parents('td').children('span:last');

should do the trick.

Edit

I didn't notice your desire to not only deal with immediate children. If you want all decendants, then you would use

$(this).parents('td').find('span:last')

I had assumed the span would always be a child of the td.

Final Edit

For posterity's sake (and to make it easier for people to find this answer in the future), since this was already accepted as the answer, Ben came up with a more elegant solution in his answer that I'm going to put here:

$(this).closest('td').find('div,span').filter(':last')

As Ben said, you must be using JQuery 1.3 for closest() to work. Good work! :-)

Marc W
children() only searches immediate children.
Ben Blank
+3  A: 

The function you're looking for is find(expr), which searches all descendant nodes:

$(this).closest('td').find('span:last')

Edit: To select the last div or span:

$(this).closest('td').find('div,span').filter(':last')

Edit 2: Mark makes an excellent point about using closest() instead of parents(), but be aware that you need to be using jQuery 1.3 for it.

Ben Blank
Thanks! Can we tweak a bit further? What if I wanted to find the last element, but instead of limiting to span, it could be a span or a div?
chipotle_warrior
You can replace span with whatever element you'd like.
Marc W
I'm talking about a span OR a div. Invalid syntax, but something like this:$(this).parents('td').filter('input').find('div,span:last')
chipotle_warrior
I'll append this to my answer. I didn't see that you mentioned selecting either a div or a span in your original question.
Marc W
Actually, that syntax is valid. But if the span comes after the div, the div still gets selected. I'm trying to select a span or a div, whichever is last.
chipotle_warrior
Oh. I'm not quite sure how to do that in a one-liner. Could do it with a few statements, though.
Marc W
I can figure it out in a multi-liner. Thanks!
chipotle_warrior
Et voila — a one-liner. :-)
Ben Blank
+4  A: 

I prefer to use closest in this case, instead parents. You don't have to worry about there being two or more 'td' elements in the ancestry.

$(this).closest('td').children("span:last")
Mark