yaharga yaharga - 5 months ago 9
jQuery Question

How to select every child of siblings array with a specific selector without a loop?

I have the following navigational menu:

<nav id="nav-primary" class="text-center">
<button id="menu-main-toggle" class="dropdown-toggle script-dependant active">Menu</button>
<ul id="menu-main" class="nav toggleable hide">
<li class="menu-item odd first"><a class="menu-link" href="//localhost:3000/index.php/about/">About</a>
</li>
<li class="menu-item even"><a class="menu-link" href="/index.php/category/bahrain/">Bahrain</a>
</li>
<li class="menu-item odd parent"><a class="menu-link" href="/index.php/category/information-technology/">Information Technology</a>
<button class="dropdown-toggle script-dependant">+</button>
<ul class="toggleable hide">
<li class="menu-item odd first parent"><a class="menu-link" href="/index.php/category/information-technology/web-development/">Web Development</a>
<button class="dropdown-toggle script-dependant">+</button>
<ul class="toggleable hide">
<li class="menu-item odd first"><a class="menu-link" href="/index.php/category/information-technology/web-development/html/">HTML</a>
</li>
<li class="menu-item even"><a class="menu-link" href="/index.php/category/information-technology/web-development/css/">CSS</a>
</li>
<li class="menu-item odd last"><a class="menu-link" href="/index.php/category/information-technology/web-development/javascript/">Javascript</a>
</li>
</ul>
</li>
<li class="menu-item even last"><a class="menu-link" href="/index.php/category/information-technology/unix/">Unix</a>
</li>
</ul>
</li>
<li class="menu-item even parent"><a class="menu-link" href="http://lipsum.com">Lorem</a>
<button class="dropdown-toggle script-dependant">+</button>
<ul class="toggleable hide">
<li class="menu-item odd first parent"><a class="menu-link" href="http://lipsum.com">Ipsum</a>
<button class="dropdown-toggle script-dependant">+</button>
<ul class="toggleable hide">
<li class="menu-item odd first last"><a class="menu-link" href="http://lipsum.com">Dolor</a>
</li>
</ul>
</li>
<li class="menu-item even last"><a class="menu-link" href="http://lipsum.com">Situs</a>
</li>
</ul>
</li>
<li class="menu-item odd"><a class="menu-link" href="/index.php/category/snippets/">Snippets</a>
</li>
<li class="menu-item even last"><a class="menu-link" href="//localhost:3000/index.php/contact/">Contact</a>
</li>
</ul>
</nav>


Every
li.parent
has a
.dropdown-toggle
button. When the button is clicked, I want to check if its
$(this).parent().siblings('.parent').child('.dropdown-toggle.active')
so that I can untoggle them when another one is toggled. I know there isn't a
child()
selector, and I can't check
siblings([selector])
's elements without an
$.each
or a loop, but I was wondering if there's a way? I want to use it in an if statement so I need it to be short and not heavy on processing.

Edit: Just ended up using
$(this).parent().siblings('.parent').children('.active')
. Thought
siblings()
would be an array and I'd have to iterate through it. Apparently it's an object and I could simply using
children()
on every element in the object.

Answer

Use find

$(this).parent().siblings('.parent').find('.dropdown-toggle.active')
Comments