1

I have a setup like this.

        var element = $(".module-panel", this.el);
        $('.module-panel:not('+element+')').hide();

What I'm trying to do is hide all divs with the class .module-panel except the one that is stored inside the var element.

2
  • I'd just hide them all, and then do element.show() afterwards Commented Mar 20, 2012 at 15:10
  • 1
    $('.module-panel').not(element) Commented Mar 20, 2012 at 15:11

2 Answers 2

3

you can use .not()

var element = $(".module-panel", this.el);
$('.module-panel').not(element).hide();

you will need jquery 1.4 or higher because element is a jquery object:

.not( jQuery object )

jQuery object - An existing jQuery object to match the current set of elements against.

Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way to do this is with .not()

var element = $(".module-panel", this.el);
$('.module-panel').not(element).hide();

Using :not() is entirely possible as well, and although the difference is miniscule, if there are multiple elements you're matching against, it could make a difference for the favorable CSS3 selector :not().

jQuery creates a method .not() which filters out any element passed to it. CSS3 has a pseudo selector :not() which allows the browser to do the work, making it slightly faster, but not as versatile.

Test results here: http://jsfiddle.net/iredmedia/jPNYK/

The reason your query doesn't work is because you are concattenating a jQuery object into a query string. In order to make your query work, you must pass it a specific element id/class string, as in

var element = '.doNotReturn',
    excluded = $('#eltFamily:not(' + element + ')').nextMethod();

Hope this helps you understand :not() vs .not();

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.