{"id":2155,"date":"2022-08-30T15:22:30","date_gmt":"2022-08-30T15:22:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/if-element-contains-child-elements-run-function-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:30","modified_gmt":"2022-08-30T15:22:30","slug":"if-element-contains-child-elements-run-function-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/if-element-contains-child-elements-run-function-collection-of-common-programming-errors\/","title":{"rendered":"If element contains child elements run function-Collection of common programming errors"},"content":{"rendered":"<p>I have a carousel on my site, and if there are no elements my jquery seems to crash.<\/p>\n<p>My HTML output without elements is&#8230;<\/p>\n<pre><code>\n     <\/code><\/pre>\n<pre>\n     <img decoding=\"async\" alt=\"Left Arrow\" src=\"_includes\/images\/larr.png\" \/>\n     <img decoding=\"async\" alt=\"Right Arrow\" src=\"_includes\/images\/rarr.png\" \/>\n\n<\/pre>\n<p><code>And my jQuery is...<\/code><\/p>\n<pre><code><code>$('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');\n<\/code><\/code><\/pre>\n<p>Is it possible to say if UL contains LI then run the function?<\/p>\n<ol>\n<li>\n<pre><code>if($('.gallery-viewport-twobedroomapartment ul &gt; li').length &gt; 0) {\n   $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>To know if there are any elements in a jQuery collection use <code>length<\/code>.<\/p>\n<pre><code>if ($(elements).length) {\n  \/\/ there are elements\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>var liCount = $('ul li').length;\n\nif (liCount &gt; 0) {\n  $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');\n}\n<\/code><\/pre>\n<p>Boom!<\/p>\n<\/li>\n<li>\n<p>You can use <code>.children()<\/code> and <code>length<\/code>:<\/p>\n<pre><code>if ($(\".gallery-viewport-twobedroomapartment\").children(\"ul\").children(\"li\").length &gt; 1)\n    $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-04 00:53:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a carousel on my site, and if there are no elements my jquery seems to crash. My HTML output without elements is&#8230; And my jQuery is&#8230; $(&#8216;.gallery-viewport-twobedroomapartment&#8217;).carousel(&#8216;#simplePrevious&#8217;, &#8216;#simpleNext&#8217;); Is it possible to say if UL contains LI then run the function? if($(&#8216;.gallery-viewport-twobedroomapartment ul &gt; li&#8217;).length &gt; 0) { $(&#8216;.gallery-viewport-twobedroomapartment&#8217;).carousel(&#8216;#simplePrevious&#8217;, &#8216;#simpleNext&#8217;); } To know [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2155","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2155"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2155\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}