1

I have a function to hide all divs on the page except one div.

// hide all div exceept div1
function hideAllExcept()
{
  $('div:not(#div1)').slideUp(800);
}

or

// hide all div exceept 'thisdiv' 
function hideAllExcept()
{
  $('div:not("#div1")').slideUp(800);
}

The above works fine (difference is first function doesn't have "" around #div1). However, I would like to pass a parameter in the hideAllExcept function to dynamically specify which div to not hide. So I changed the function to:

// hide all div exceept 'thisdiv' 
function hideAllExcept(thisdiv)
{
  $('div:not(thisdiv)').slideUp(800);
}

if i call the function using: hideAllExcept('#div1') or hideAllExcept("#div1") it doesn't work. It seems that $('div:not(thisdiv)') still selects all divs, it doesn't exclude thisdiv.

Any ideas? Many thanks

3 Answers 3

4

Change it to:

function hideAllExcept(thisdiv) {
  $('div:not('+thisdiv+')').slideUp(800);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks all, all answers are pretty similar so have to choose the first answer. btw, is this trick documented anywhere? I googled it before posting on SO but had no luck finding it anywhere.
It's not really a trick, but JavaScript string concatenation. You're dynamically building the selector.
4
$('div').not(thisdiv).slideUp(800);

6 Comments

I think you mean $('div').not(thisdiv).slideUp(800); But I like this better than the others. It will allow passing an element reference, rather than just a string.
dang though I got the fix in before anyone noticed, but yes, the .not() function is better since it can take the full variety of inputs
Did I just read that wrong? I could have sworn that said filter, but I don't see an edit.... Weird.
Okay, so I'm not just going crazy. Good :)
This is slower than using the :not(...), since you would select all DIV elements and then filter them with the .not(...) function.
|
2
var divid='div:not('+thisdiv+')';
$(divid).slideUp(800);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.