Dima Gimburg Dima Gimburg - 2 months ago 15
jQuery Question

Check if jQuery object has html element in it

Maybe I miss something but anywhere I read it is suggested that if I want to check whether a jQuery constructor returns a jQuery object is done by:

if(jQuery('.something').length){...}


but how would I validate if it is an html element in the jQuery object that it returns?

what I mean is that this also returns true:

if(jQuery(44551).length){...}


although it returns jQuery object it doesn't have html element in it.

Answer Source

You could use is() to check if the collection contains an element matching a certain selector

var someelement = $(....); // unknown

if ( someelement.is('.something') ) {...

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.