jQuery isChildOf - is an element a child of another element

Here i s a really quick note on creating a basic extension to jQuery to check if an element is a child of another element.

jQuery Function


  
(function($) {  
    $.fn.extend({  
        isChildOf: function (filter) {  
            return $(filter).find(this).length > 0;  
        }  
    });  
})(jQuery);   
  

Example HTML

Consider the simple HTML below.


 
div id="parent"
  div> 
    id="child"
    p> 
  div> 
div> 
  

Usage

Some basic usage of the function.

 
if($('#child').isChildOf('#parent')){ 
 ....