Is there such thing as a relative jQuery selector?

Eric picture Eric · Aug 23, 2010 · Viewed 9.2k times · Source

I have a reference to a jquery object with the this variable. I am looking for a way of applying the child selector to the object.

I'm using $(this).find('table > tbody > tr > td'), but what I'm aiming for is something more like $('[Value of $(this) goes here somehow] > table > tbody > tr > td').

I realise that I can do $(this).children('table').children('tbody').children('tr').children('td'), but I was wondering if there was some syntactic sugar I could use here.

Answer

Nick Craver picture Nick Craver · Aug 23, 2010

You can start with a child selector (>) when using .find() as well, like this:

$(this).find('> table > tbody > tr > td')

It's an often overlooked use case, but it works just great for what you're after.