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…


     
     Left Arrow
     Right Arrow

And my jQuery is...

$('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');

Is it possible to say if UL contains LI then run the function?

  1. if($('.gallery-viewport-twobedroomapartment ul > li').length > 0) {
       $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
    }
    
  2. To know if there are any elements in a jQuery collection use length.

    if ($(elements).length) {
      // there are elements
    }
    
  3. var liCount = $('ul li').length;
    
    if (liCount > 0) {
      $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
    }
    

    Boom!

  4. You can use .children() and length:

    if ($(".gallery-viewport-twobedroomapartment").children("ul").children("li").length > 1)
        $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');