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?
You can use
.find()(any descendant), or.children()(only direct children), like this:.find()is equivalent to what you currently have,.children()would be equivalent to this: