views:

436

answers:

1

Hello!

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.

+4  A: 

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");
});
karim79
those divs have classes and in those classes it is 1 to 5.
Elijah Woods
Use also the jQuery object accessor INDEX if you don't have an ID. It returns the index (based zero) of that div in the page
ranonE
Yes, but how do make sure that all my divs with the classes 1 to 5 are slided up? This only makes sure 1 div with someClass is slided up and not all.
Elijah Woods
@Elijah Woods - then maybe use the starts with attribute filter, e.g. $('div[class^=someClass] not:('#idofseconddiv')") where someClass is what your classes start with, before the number, e.g. myClass if you have myClass1, myClass2, etc.
karim79
Great karim79! The starts with attribute filter was awesome :p
Elijah Woods