If element contains child elements run function-Collection of common programming errors
I have a carousel on my site, and if there are no elements my jquery seems to crash.
My HTML output without elements is…
And my jQuery is...
$('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
Is it possible to say if UL contains LI then run the function?
-
if($('.gallery-viewport-twobedroomapartment ul > li').length > 0) { $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext'); } -
To know if there are any elements in a jQuery collection use
length.if ($(elements).length) { // there are elements } -
var liCount = $('ul li').length; if (liCount > 0) { $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext'); }Boom!
-
You can use
.children()andlength:if ($(".gallery-viewport-twobedroomapartment").children("ul").children("li").length > 1) $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
Originally posted 2014-01-04 00:53:12.