Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to not alter your HTML if possible, so that you wouldn't have to deal with styling headaches. Unfortunately this means the JS is a bit more hard coded than I would like.</p> <p><strong>HTML:</strong></p> <pre><code>&lt;div class="topCats"&gt; &lt;h4&gt;&lt;a href="#" class="openBox"&gt;Most Searched Categories&lt;/a&gt;&lt;/h4&gt; &lt;/div&gt; &lt;div id="topCatsContainer" class="accordianContent"&gt; content 1 &lt;/div&gt; &lt;div class="allCats"&gt; &lt;h4&gt;&lt;a href="#" class="closed"&gt;All Categories&lt;/a&gt;&lt;/h4&gt; &lt;/div&gt; &lt;div id="allCatsContainer" class="accordianContent"&gt; content 2 &lt;/div&gt; </code></pre> <p>The HTML is the same except I added a class to each of your content div.</p> <p><strong>JS:</strong></p> <pre><code>$('#allCatsContainer').css("display", "none"); $('.topCats h4 a, .allCats h4 a').click(function() { var content = $(this).parent().parent().next(); if(content.is(':visible')) { content .slideUp(500) .addClass('closed') .removeClass('openBox'); } else { content .parent() .find('.accordianContent:visible') .slideUp(500) .addClass('closed') .removeClass('openBox'); content.slideDown(500).removeClass('closed').addClass('openBox'); } }); </code></pre> <p>Basically the JS finds the content that matches the link that was clicked. If the content was visible then it just closes itself. If it wasn't visible then we close whichever was visible and open the clicked content.</p> <p>If you notice I am toggling both the <code>openBox</code> and <code>closed</code> class, which can be redundant but I wasn't sure if your styling relied on it or not.</p> <p>In general this functionality can be accomplished many ways and if you are willing to change your HTML a bit we could have the JS side look a bit more presentable :) Otherwise you can just use this implementation and avoid any styling headaches.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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