I am struggling with how to select a particular element with jQuery. Conditions outlined here:
- At the relevant point in my function,
thisreferences aspan. - That
spanmay be a child, grand child, or great grand child, but somewhere up the hierarchy, it’s contained by atd. - I’d like to select the last
spaninside of thetd. - The
spanmay bethis, or it may be anotherspan. - The
spantags to be selected is a direct child of the containingtd, solast-childis 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.
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
I had assumed the
spanwould always be a child of thetd.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:
As Ben said, you must be using JQuery 1.3 for
closest()to work. Good work! 🙂