jquery if div id has children
Another option, just for the heck of it would be:
if ( $('#myFav > *').length > 0 ) {
// do something
}
May actually be the fastest since it strictly uses the Sizzle engine and not necessarily any jQuery, as it were. Could be wrong though. Nevertheless, it works.
This snippet will determine if the element has children using the :parent
selector:
if ($('#myfav').is(':parent')) {
// do something
}
Note that :parent
also considers an element with one or more text nodes to be a parent.
Thus the div
elements in <div>some text</div>
and <div><span>some text</span></div>
will each be considered a parent but <div></div>
is not a parent.
if ( $('#myfav').children().length > 0 ) {
// do something
}
This should work. The children()
function returns a JQuery object that contains the children. So you just need to check the size and see if it has at least one child.