I have the following jQuery code:
$('#menu .selected').removeClass('selected');
$('#homel').addClass('selected');
$('#home').slideDown(150, function() {
$('#public').slideUp(200);
$('#login').slideUp(200);
$('#faq').slideUp(200);
});
Which, as you can guess, hides and shows div boxes. In that example #homel is the Home link, and #home is the home div. This looks ok, but if I want to add a new box, for example, #registerI have to add here and on every other box code (#public, #login and #faq) the code: $('#register').slideUp(200);
Is there any way that can be simplified and made generic for all?
Here is a different solution. I gave all h3 divs a common class and select them with one selector. This way it’s you don’t have to change your code everytime you add another div. There is also a function for
slideUp. Now all routes can use same function, no need for lots of anonymous functions.This is the final result;
HTML
Javascript
And working example of final result is here.