15

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.

3 Answers 3

29

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Why didn't I think of that?
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: Something like this is pointless in CSS because there's no context. In JavaScript/jQuery selectors, it makes more sense.
@YiJiang - Here's an example to play with :) jsfiddle.net/nick_craver/qUfJq
@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
|
8

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') 

7 Comments

That is .find(), just more/extra steps to get there first :)
@Nick: yes, but the difference is negligible and I think it looks neater, not to mention 5 or 6 characters shorter ;)
@Andy - That's the thing, that difference can really add up, take a look at the code path: 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.
I find find() more explanatory anyway
@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.
|
8

An alternative way would be passing a second parameter $('selector', context), which defines a context for search.

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function.

$( "div.foo" ).click(function() { $( "span", this ).addClass( "bar" ); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.