tags:

views:

33

answers:

2

I have an event handler that needs to do something to one of its children, based on some saved data. So what I'm currently doing is something like this:

// Get the ID of 'this'
item_id = $(this).attr('id');

// building a selector like $('#item1 .child_3')
$('#' + item_id + ' .child_' + spVal).addClass('blah');

But this seems a little cumbersome. I've tried:

$(this + ' .child_' + spVal).addClass('blah');

and it doesn't work, which doesn't really surprise me. Is there a better way to do this then the successful way I've outlined above?

+2  A: 

You can select the children like this as well:

$(this).children('.child_' + spVal).addClass('blah');
Daff
+4  A: 

You can use .find() (any descendant), or .children() (only direct children), like this:

$(this).find('.child_' + spVal).addClass('blah');

.find() is equivalent to what you currently have, .children() would be equivalent to this:

$('#' + item_id + ' > .child_' + spVal).addClass('blah');
Nick Craver
I've never had the cause to use .find before - thanks for the new toy :-)
Stomped