Note that there are some explanatory texts on larger screens.

plurals
  1. POImprove JavaScript - jQuery code for active menu and show/hide div
    primarykey
    data
    text
    <p>I am wondering how can this code be improved? I have 3 menu buttons in navigation. The first one is active by default. On click, other buttons are assigned with class active.</p> <pre><code>&lt;div class="mobile-nav"&gt; &lt;ul&gt; &lt;li&gt;&lt;input class="mobile-nav-tab active" id="search-tab" type="button"/&gt;&lt;/li&gt; &lt;li&gt;&lt;input class="mobile-nav-tab" id="menu-tab" type="button"/&gt;&lt;/li&gt; &lt;li&gt;&lt;input class="mobile-nav-tab" id="members-tab" type="button"/&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>each of these buttons will show a certain div and hide the others.</p> <pre><code>&lt;div class="mobile-nav-content"&gt; &lt;div id="search"&gt; &lt;/div&gt; &lt;div style="display:none" id="menu"&gt; &lt;br&gt;menu &lt;br&gt;menu &lt;/div&gt; &lt;div style="display:none" id="members"&gt; &lt;br&gt;members &lt;br&gt;members &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I have this code and it is working fine.</p> <pre><code>$(document).ready(function(){ $('.mobile-nav-tab').click(function(){ $('.mobile-nav-tab').removeClass('active'); $(this).addClass('active'); if ($('#search-tab').hasClass('active')){ $('#search').slideDown(); } else{ $('#search').hide(); } if ($('#menu-tab').hasClass('active')){ $('#menu').slideDown(); } else{ $('#menu').hide(); } if ($('#members-tab').hasClass('active')){ $('#members').slideDown(); } else{ $('#members').hide(); } }); }); </code></pre> <p>I found out that i can shorthand if-else parts with Ternary operator. But i wonder is there any other way to improve it and make it shorter?</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.
 

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