Atal Shrivastava
Atal Shrivastava

Reputation: 702

sliding multiple elements on click

i have a vertical menu bar which contains sub menu. There are 9-10 menus and each menu contains 3 sub menus.

what i need if menu 1 is open and someone click on menu 3 menu 1 should close & menu 3 will open

 $('#nav li a').click(function(){
    var sds = document.getElementById("dum");
    if(sds == null){
    ;
    }
    var sdss = document.getElementById("dumdiv");
    if(sdss == null){



    }
    if(sdss != null){
            var s = $(this).attr('id');
            var imgid=$("#"+s+" img").attr('id');
            var imgsrc=$("#"+imgid+"").attr('src');
            if(imgsrc=="images/insert.GIF")
            {
                $("#"+imgid+"").attr('src','images/remove.GIF');
                $(this).next().slideDown(400);
                $("#"+s+"").css("background-color","#142878");
            }
            else
            {
                $("#"+imgid+"").attr('src','images/insert.GIF');
                $(this).next().slideUp(400);
                $("#"+s+"").css("background-color","#2d539a");
            }
    }
        });

here's the fiddle

Upvotes: 1

Views: 166

Answers (3)

Jai
Jai

Reputation: 74738

Try this:

 $('.count').slideUp(400);     //<----add this line
 $(this).next().slideDown(400);

Demo


Updates:

try this:

if (imgsrc == "images/insert.GIF") {
    $("#" + imgid + "").attr('src', 'images/remove.GIF');
    $('.count').slideUp(400);
    $(this).next().slideDown(400);
    $("#" + s + "").css("background-color", "#142878");
} else {
    $("#" + imgid + "").attr('src', 'images/insert.GIF');
    $('.count').slideUp(400);  //<--------------------add here
    $(this).next().slideDown(400);  //<---------------and here too.
    $("#" + s + "").css("background-color", "#2d539a");
}

Upvotes: 1

Felix
Felix

Reputation: 38112

Add this to the beginning your code:

$('#nav li a').click(function(){
    $(this).closest('li').siblings('li').find('.count').slideUp();
    // Rest of the code here

Updated Demo

Upvotes: 2

Linga
Linga

Reputation: 10573

Use

$('a').next().slideUp(400); 

If you click on any menu, this will close the current menu and opens the new one

Here is the updated Fiddle

Upvotes: 2

Related Questions