if ( $(element).parent('table').length ) { /* parent is table */ }
if ( $(element).closest('table').length ) { /* parent/N-parent is table */ }
jQuery(function($) {
var $element = $(selector),
// You can use .closest if you are not looking for a direct parent.
// http://api.jquery.com/closest/
isChildOfTable = $element.parent('table').length;
if ( isChildOfTable ) {
}
});
Just to add a .parents
vs .closest
discussion.
The problem with the jsperf is that it's trying to fetch the element rater then checking the length property. And the result is there fore not concluded in the tests:
