views:

79

answers:

2

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.

+2  A: 

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.

Nick Craver
Why didn't _I_ think of that?
Eric
You can actually use the `>` selector without anything in front of it? *Scratch head* That's certainly not valid CSS, and I don't see it getting mentioned in the API pages... Hmmmm... will give it a try now
Yi Jiang
@Yi Jiang: Something like this is pointless in CSS because there's no context. In JavaScript/jQuery selectors, it makes more sense.
Andy E
@YiJiang - Here's an example to play with :) http://jsfiddle.net/nick_craver/qUfJq/
Nick Craver
@Nick Oh wow, thanks. You didn't really have to, you know - I can do all the fiddling I want with the Firebug console :D
Yi Jiang
@Nick Craver: How did you get the jsFiddle username?
Eric
@Eric - It'll be in beta very soon as you'll be able to create a user...it's a manual process at the moment the guys behind it were kind enough to run for me. It's excellent job by the MooTools guys on that one, it's a pair of them that are the main drivers behind fiddle.
Nick Craver
A: 

As Nick said, you can use find(), or you can use selector context:

$('> table > tbody > tr > td', this)

// Is the equivalent of
$(this).find('> table > tbody > tr > td')
Andy E
That *is* `.find()`, just more/extra steps to get there first :)
Nick Craver
@Nick: yes, but the difference is negligible and I think it looks neater, not to mention 5 or 6 characters shorter ;)
Andy E
@Andy - That's the thing, that difference can really add up, take a look at the code path: http://github.com/jquery/jquery/blob/master/src/core.js#L62 With `.find()` you do 1 `if()` check (to get `$(this)`), then go Sizzle, with the `$(selector, this)` format you do 7 `if()`'s and 2 regexes to get to the same place...if you're looping that can really add up. If you're not looping I agree, but the performance is a factor when running this quite a bit.
Nick Craver
I find `find()` more explanatory anyway
Eric
@Nick: I'd not looked at the code, that does sound rather drastic. It seems like it could be optimized better than that, if you ask me. Looking at the overloads in the documentation, it should be as simple as a condition to check that the second argument to jQuery() is a DOM element and pass the whole thing over to *find()*. Not sure why the 2 regexes are necessary at all. *if* conditions don't amount to much, but invoking the regex compiler is a fair argument for better performance.
Andy E
@Eric: I would have answered with *find()* too, if @Nick hadn't already. I think it's worth letting you know all your options, which is why I added this answer ;) don't forget to mark Nick's answer as accepted :)
Andy E
@Andy: yep, he answered so fast that I couldn't accept the answer when he posted it - I had to wait 10 minutes.
Eric