Note that there are some explanatory texts on larger screens.

plurals
  1. POToggle multiple divs one by one jquery
    primarykey
    data
    text
    <p>What i am trying to do is toggle between different div's. It's kinda hard to explain but i'll give it a try.</p> <hr> <p>When the page loads there would be div that is visible and 4 with display:none. And there would be a menu. link 1 would show the first div and hide all others. Then when clicking link 2 the div that is visible will hide and div2 would show. When clicking link 3 the div that is visible will hide and div3 would show and so on. Basically only one div shown at a time. </p> <hr> <p>I wrote this but it works only when there are 2 divs.</p> <pre><code>$(function () { $('#link').click(function () { $('#div1, #div2').toggle(); }); }); </code></pre> <p>but this would only show the hidden div and hide the one that is shown.</p> <p>Okay, i did it and found out that it can be done much easier. Here's what i did. It's not very elegant but it works.</p> <pre><code>$(document).ready(function () { $('.slidingDiv').hide(); $('.show_hide').show(); $('.show_hide').click(function () { $('.slidingDiv').slideToggle(); $('.slidingDiv2').hide('slow'); $('.slidingDiv3').hide('slow'); $('.slidingDiv4').hide('slow'); $('.slidingDiv5').hide('slow'); }); }); $(document).ready(function () { $('.slidingDiv2').hide(); $('.show_hide2').show(); $('.show_hide2').click(function () { $('.slidingDiv2').slideToggle(); $('.slidingDiv').hide('slow'); $('.slidingDiv3').hide('slow'); $('.slidingDiv4').hide('slow'); $('.slidingDiv5').hide('slow'); }); }); $(document).ready(function () { $('.slidingDiv3').hide(); $('.show_hide3').show(); $('.show_hide3').click(function () { $('.slidingDiv3').slideToggle(); $('.slidingDiv').hide('slow'); $('.slidingDiv2').hide('slow'); $('.slidingDiv4').hide('slow'); $('.slidingDiv5').hide('slow'); }); }); $(document).ready(function () { $('.slidingDiv4').hide(); $('.show_hide4').show(); $('.show_hide4').click(function () { $('.slidingDiv4').slideToggle(); $('.slidingDiv').hide('slow'); $('.slidingDiv2').hide('slow'); $('.slidingDiv3').hide('slow'); $('.slidingDiv5').hide('slow'); }); }); $(document).ready(function(){ $('.slidingDiv5').hide(); $('.show_hide5').show(); $('.show_hide5').click(function () { $('.slidingDiv5').slideToggle(); $('.slidingDiv').hide('slow'); $('.slidingDiv2').hide('slow'); $('.slidingDiv3').hide('slow'); $('.slidingDiv4').hide('slow'); }); }); </code></pre> <p>And <code>&lt;a href="#" class="show_hide"&gt;&lt;span class="nav"&gt;link&lt;/span&gt;&lt;/a&gt;</code></p>
    singulars
    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