JQuery Toggle Slide – display element when other elements toggle closed

JSFiddle: http://jsfiddle.net/ncuacvcu/

DIV home is displayed by default.

When I click on LINK one/LINK two, DIV one/DIV two replaces DIV home. When I click on LINK one/LINK two again, DIV one/DIV two toggles shut, leaving an empty white space. How do I get DIV home to display again at that moment?

At the same time, if DIV one is open and I click on LINK two, I want DIV one to be replaced by DIV two (i.e. without going through DIV home).

Here's what I tried (and some variations), but I can't get it to work:

               } else{

Thanks in advance for any tips!

1 answer

  • answered 2018-01-14 12:28 The_Death_Raw

    You need to use the callback of slideToggle and check if the div with class home is visible or not then you show it.



    $(function() {
        $("a#one_toggle").click(function() {
             $(".one").slideToggle('slow', function() {
        $("a#two_toggle").click(function() {
             $(".two").slideToggle('slow', function() {
    function showHome(elementToCheck) {
        // now we know if the div to check is visible or not
        if (!$(elementToCheck).is(':visible')) {
            // the div is not visible so we show it
    .two {
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="#" id="one_toggle">link one</a>
    <a href="#" id="two_toggle">link two</a>
    <div class="home hideall">
    <div class="one hideall">
    <div class="two hideall">