Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery hide/show 2 menus
    text
    copied!<p>Good day everyone!</p> <p>I am having the following issue: I have a menu for one product and another menu for another product. It goes something like this:</p> <p>Product1 | Product2</p> <p>Home Option1 Option2 Option3 | Option1 Option2 Option3</p> <p>where first 3 options are for Product1 and the last 3 options are for Product2. I only need to see the menu for one product.</p> <p>My HTML goes smth like this (simplified of course):</p> <pre><code>&lt;div class="products"&gt; &lt;a href="#" id="product1"&gt;Product1&lt;/a&gt; &lt;a href="#" id="product2"&gt;Product2&lt;/a&gt; &lt;/div&gt; &lt;ul id="menu1"&gt; &lt;li&gt;Option1&lt;/li&gt; &lt;li&gt;Option2&lt;/li&gt; &lt;li&gt;Option3&lt;/li&gt; &lt;/ul&gt; &lt;ul id="menu2"&gt; &lt;li&gt;Option1&lt;/li&gt; &lt;li&gt;Option2&lt;/li&gt; &lt;li&gt;Option3&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>And jQuery goes smth like this:</p> <pre><code>$(document).ready( function() { $("#product1").addClass("active"); $("#menu2").hide(); $("#product2").click(function () { $("#menu1").hide(); $("#menu2").show(); $("#product1").removeClass("active"); $("#product2").addClass("active"); }); $("#product1").click(function () { $("#menu2").hide(); $("#menu1").show(); $("#product2").removeClass("active"); $("#product1").addClass("active"); }); }); </code></pre> <p><strong>Problem:</strong> When I first load the page everything is ok. When I click on #product2 for instance, it does everything that is specified in the function:</p> <pre><code> $("#product2").click(function () { $("#menu1").hide(); $("#menu2").show(); $("#product1").removeClass("active"); $("#product2").addClass("active"); }); </code></pre> <p>but it also executes the first two lines:</p> <pre><code> $("#product1").addClass("active"); $("#menu2").hide(); </code></pre> <p>And I get both #product1 and #product2 active and both menus hidden. Any ideas? Thanks in advance!</p>
 

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