What code do I need to collapse a div when another is open?

I use a simple bit of code to make the div collapse, here it is:

<script language="JavaScript">
    <!--   
        function expand(param)   
        {   
            param.style.display=(param.style.display=="none")?"":"none";   
        }   
    //-->  
</script>

What code do I add so that it recognizes when one div is open to collapse the previous div?
here is the link I would use:

<a href="javascript:expand(document.getElementById('div1'))">Link 1</a>  
      <div id="div1" width="300px" style="display:none"></div>

Any ideas?

+3
source share
2 answers

If you wanted to use jQuery, the selector of your interest is something like

$('div#parent-container > div').filter(':visible');

For example, if I were to demonstrate the next and previous, I would do it something like this . Using target links, it will work by adding an ID to divsand referencing the attributes hreffor `anchors'. (now included in the example)


Something mess with :

$(function(){
    //Reference Object
    var $divs = $('div > div');
    //Buffer for selected variable
    var $selected = 0;
    //Show first
    $divs.eq(0).show();


    $('#next').click(function(){
        //Update selected var
         $selected = $divs.filter(':visible');
        //Save next to variable
        var $next = $selected.next();
        //Change Visibility
        toggle($next);
        //Prevent Default
        return false;
    });

     $('#prev').click(function(){
        $selected = $divs.filter(':visible');
        var $prev = $selected.prev();
        toggle($prev);
        return false;
    });

    $('a').click(function(){
        $selected = $divs.filter(':visible');
        var selector = $(this).attr('href');
        if(selector == '#') return false;
        toggle( $( selector ) );
        return false;
    });


    var toggle = function($toggle){
     if(!$toggle.length) return false;
         $selected.hide();
         $toggle.show();   
     }
});

<!--Simple Implementation and dependancies-->
<a id="prev" href="#">Prev</a>
<a id="next" href="#">Next</a>
<a href="#item-4">Show Item Four</a>

<div>
    <div id="item-1">One</div>
    <div id="item-2">Two</div>
    <div id="item-3">Three</div>
    <div id="item-4">Four</div>
    <div id="item-5">Five</div
    <div id="item-6">Six</div>
</div>

div > div {
 font-size:5em;
 width:auto;
 text-align:center;
 padding:20px 0;   
 display:none;
}
+2

, jQuery. jsfiddle.

http://jsfiddle.net/mrtsherman/uqnZE/

html

<div class="category">A
    <div class="artists">Apple<br/>Ace<br/>Ants<br/></div>
</div>
<div class="category">B
    <div class="artists">Bee<br/>Bop<br/>Book<br/></div>
</div>
<div class="category">C
    <div class="artists">Cake<br/>Chimp<br/>Charles<br/></div>
</div>

:

$(".category").click( function() {    
    $(".artists").hide();
    $(this).children(".artists").show();
});

, , divs, , div , . .

+4

Source: https://habr.com/ru/post/1795346/


All Articles