Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery: toggling the siblings
    primarykey
    data
    text
    <p>I want to toggle the next sibling of a link in a list like this,</p> <pre><code>&lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;2&lt;/a&gt; &lt;ul class="selected"&gt; &lt;li&gt;&lt;a href="#"&gt;2.1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;2.2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;2.3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;4&lt;/a&gt; &lt;ul class="selected"&gt; &lt;li&gt;&lt;a href="#"&gt;4.1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;4.2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;4.3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt;5&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>and this is my jquery,</p> <pre><code>$(document).ready(function(){ $("a").each(function () { if ( $(this).siblings().size() &gt; 0) { $(this).append("&lt;span class='has-child'&gt;has child&lt;/span&gt;"); $(this).toggle( function (){ $("ul").removeClass("showme"); $(this).siblings(".selected").addClass("showme"); //$(this).next().css({display: "block"}); }, function (){ $(this).siblings(".selected").removeClass("showme"); //$(this).next().css({display: "none"}); }); } }); $('ul &gt; li &gt; ul &gt; li:last-child &gt; a').css('background','yellow'); }); </code></pre> <p>the css,</p> <pre><code>ul &gt; li &gt; ul { display:none; } ul &gt; li:hover ul{ display: block; } .showme { display: block; } </code></pre> <p>first of all, it seems fine - I can toggle the targeted sibling, but why after the first toggle at any parent, I have to click twice sometime to hide other siblings when I click/ toggle at the current parent?</p> <p>here is the live site if I am not explaining it clearly, <a href="http://lauthiamkok.net/tmp/jquery/toggle/" rel="nofollow noreferrer">http://lauthiamkok.net/tmp/jquery/toggle/</a></p> <p>Many thanks, Lau</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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