I currently have a menu containing 3 links, which opens hidden div’s relevent to itself and use this jquery code (see below) but would like it that if a div is aready open when a second div is opened that its closes the original opened div …
ie if “foobox” is open and then user clicks “foo2” to open “foobox2” “foobox” will close
$('#foo').toggle(function(){
$('#foobox').animate({marginLeft: '354'}, 1000);
},
function(){
$('#foobox').animate({marginLeft: '0'}, 1000);
});
$('#foo2').toggle(function(){
$('#foo2box').animate({marginLeft: '354'}, 1000);
},
function(){
$('#foobox3').animate({marginLeft: '0'}, 1000);
});
$('#foo3').toggle(function(){
$('#foobox3').animate({marginLeft: '354'}, 1000);
},
function(){
$('#foobox3').animate({marginLeft: '0'}, 1000);
});
as usual thank you in advance.
When you are opening one, add a new class to it that indicates that it’s the active one. And every time you open something, close the active one.
Also, I would recommend changing your HTML and jQuery so you only need one event handler.
For example instead of this:
You could do:
With the following jQuery: