Search code examples
jqueryhtmlslideup

slideUp multiple divs


How can i make sure that several divs are slided up? Right now I have this function:

$("div#personal").click(function() { 
  $("div.1").slideUp("slow", function () { $("div.2").slideDown("slow") } ); 
});

What i would want to do is to make sure that all divs that are named:

  1. div.1
  2. div.2 EXCEPT THIS ONE
  3. div.3
  4. div.4
  5. div.5

is slided up, except the second div.


Solution

  • You can use the not selector to prevent one (or more) of them from sliding up:

    $("div#personal").click(function() { 
      $("div :not('#idofseconddiv'").slideUp("slow");
    });
    

    It would be a good idea to give those divs a common css class so the selector does not select all divs in your page:

    $("div#personal").click(function() { 
      $("div.someClass :not('#idofseconddiv')").slideUp("slow");
    });
    

    If you are using numbered class names on your divs, you might want to consider using the starts with attribute filter:

    $("div#personal").click(function() { 
      $("div[class^=yourClass] :not('#idofseconddiv')").slideUp("slow");
    });