jquery: if (target) is child of ('.wrapper') then (do something)
if($(target).parents('.wrapper').length > 0) {
//do something...
}
.has()
is maybe the mose convenient syntax:
if( $('.wrapper').has($(target)) ) {
// do something
}
Even more 'powerful' (in terms of performance) is $.contains()
. So an ideal algorithm should look like:
var $wrapper = $('.wrapper'),
$target = $(this).attr('href');
if( $.contains($wrapper[0], $target[0]) ) {
// do something
}
Reference: .has(), $.contains()
Here's a tidier way: bind it as a jQuery plugin. You might find it be easier to understand and use.
$.fn.isChildOf = function(element)
{
return $(element).has(this).length > 0;
}
Usage:
if ( $('.target').isChildOf('.wrapper') ) {
//do all the things.
}
Small change to Jacob's code, if the child is deeper than one level in.
if($(target).parents('.wrapper').length) {
//do something...
}
you can use parent or parents method like in the links
http://jsfiddle.net/6BX9n/
http://jsfiddle.net/6BX9n/1/