The easiest approach is to provide an additional common class name to all the elements that you are trying to shift up / down and then connect the actual bindings to the elements using the href attribute on the anchor. The next javascript bit (with changes in HTML) will perform the same functions as your current javascript, and also make it easier to add additional sliders:
# the html <a class='about slide-control' href='#about'>toggle the about section</a> <a class='info slide-control' href='#info'>toggle the info section</a> <a class='contact slide-control' href='#contact'>toggle the contact section</a> <div class='slider' id='about'> this is the about section </div> <div class='slider' id='info'> this is the info section </div> <div class='slider' id='contact'> this is the contact section </div> # the javascript $('a.slide-control').click(function() { $('div.slider').slideUp(); var target = $(this).attr('href'); $(target).slideDown('slow'); });
You can add a new slider by simply adding the correct HTML:
<a class='slide-control' href='#new_section'>Tell me more about this new section!</a> <div class='slider' id='new_section'> This is a great paragraph about the new section on our website! </div>
Alternatively, you can use existing classes and simply add a .slideUp method .slideUp to your other elements in front of your .slideDown :
$('a.about').click(function() { $("#info").slideUp('slow'); $("#contact").slideUp('slow'); $("#about").slideDown('slow'); });
source share