Skip to content Skip to sidebar Skip to footer

Expand And Collapse Div

The expand and collapse functionality is currently working fine but I'd like to modify the functionality so when I click on Expand 1, and then click on Expand 2, that Expand 1 shou

Solution 1:

You can do,

$(".header").click(function () {
    $header = $(this);
    $content = $header.next();
    $(".content").not($content).slideUp().prev().text("Expand");
    $content.slideToggle(500, function () {
        $header.text(function () {
            return$content.is(":visible") ? "Collapse" : "Expand";
        });
    });

});

Fiddle

  • Collapse all, except the current one using the code $(".content").not($content).slideUp().prev().text("Expand");

Solution 2:

First you need to collapse other if any already expanded. I updated jsfiddle and it is working now. jsfiddle Try this:

$(".header").click(function () {

CollapseAll(this);    

$header = $(this);
//getting the next element
$content = $header.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$content.slideToggle(500, function () {
    //execute this after slideToggle is done//change text of header based on visibility of content div
    $header.text(function () {
        //change text based on conditionreturn $content.is(":visible") ? "Collapse" : "Expand";
    });
});

});

functionCollapseAll(obj){
$(".header").each(function(i, item){
    var that = $(this);
    if($(this).next().is(":visible") && this != obj){
        $(this).next().slideToggle(500, function () {
            //execute this after slideToggle is done//change text of header based on visibility of content div
            that.text(function () {
            //change text based on conditionreturn that.next().is(":visible") ? "Collapse" : "Expand";
            });
        });
    }
});
}

Solution 3:

You have to select the content sections of headers that were not clicked and slide them up. Something like:

$(".header").click(function () {
    $header = $(this);
     //getting the next element$content = $header.next();
     //toggle other content area slidesvar notClicked =  $('.header').not(this).next().slideUp(500);
    $content.slideToggle(500, function () {
        //execute this after slideToggle is done//change text of header based on visibility of content div$header.text(function () {
            //change text based on conditionreturn$content.is(":visible") ? "Collapse" : "Expand";
        });
    });
});

Solution 4:

You can add these two lines in your JS code to achieve desired result.

$contents = $(".header").not(this).next();
$contents.filter(":visible").slideUp(500).prev().text("Expand");

So complete JS code

$(".header").click(function () {

    $header = $(this);
    //getting the next element$content = $header.next();
    //open up the content needed - toggle the slide- if visible, slide up, if not slidedown.$content.slideToggle(500, function () {
        //execute this after slideToggle is done//change text of header based on visibility of content div$header.text(function () {
            //change text based on conditionreturn$content.is(":visible") ? "Collapse" : "Expand";
        });
    });

    // Get all content panels and Hide them$contents = $(".header").not(this).next();
    $contents.filter(":visible").slideUp(500).prev().text("Expand");

});

The idea is to select all header elements excluding this clicked header and if they are visible hide them.

Hope this helps.

Solution 5:

You can add this to the end of your click code. It collapses all of the siblings then performs your text check after the collapse.

Fiddle

$header.siblings(".header").each(function(index,obj){
    header = $(obj);
    content = header.next();
    content.slideUp(500, function(){
        header.text(function () {
            return content.is(":visible") ? "Collapse" : "Expand";
        });             
    });

});

Post a Comment for "Expand And Collapse Div"