Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your specific code, it looks like you need to use <code>this</code> to refer to the element that was clicked on and then find the parent <code>&lt;li&gt;</code> from there so you are operating on only the <code>&lt;li&gt;</code> that had the click in it:</p> <pre><code>$('#DeltaPlaceHolderLeftNavBar div &gt; div &gt; ul &gt; li &gt; a').click(function() { if($(this).closest("li").children("ul").length) { // the clicked on &lt;li&gt; has a &lt;ul&gt; as a direct child } }); </code></pre> <hr> <p>In jQuery, you can use either <code>.find("ul")</code> or <code>.children("ul")</code> depending upon whether you're looking for only immediate descendants or any descendant.</p> <p>For example, if you want to find out if a particular <code>&lt;li&gt;</code> tag that you already have a reference to has a <code>&lt;ul&gt;</code> as a direct child, then you can do this:</p> <pre><code>if ($(el).children("ul").length) { // el has a ul as an immediate descendant } </code></pre> <p>Or, if the ul can be any descendant, then you can use this:</p> <pre><code>if ($(el).find("ul").length) { // el has a ul as a descendant at any level } </code></pre> <hr> <p>If you want to just find all the <code>&lt;li&gt;</code> tags with <code>&lt;ul&gt;</code> below them, then you have these two options:</p> <p>You can get a list of all <code>&lt;li&gt;</code> tags with a <code>&lt;ul&gt;</code> anywhere inside of it like this:</p> <pre><code>var tags = $("li").filter(function() { return $(this).find("ul").length !== 0; }); </code></pre> <p>If you only want immediate descendants, you can use this:</p> <pre><code>var tags = $("li").filter(function() { return $(this).children("ul").length !== 0; }); </code></pre> <hr> <p>You can then operate on those particular <code>&lt;li&gt;</code> tags by just calling a method on the jQuery object without using the <code>if</code>:</p> <pre><code>var tags = $("li &gt; ul").addClass("hasSubMenu"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload