Jquery filters a specific element

I implement an accordion style behavior where only one element can be displayed at one point. I thought it would be easier to implement a function show(e)that displays an element ethat will be displayed and hides everything, BUT e. This saves me the hassle of tracking which item is displayed. I could just attach show(e)as a callback for each element of the accordion.

To do this, I thought that the only way is to use .each()all ad elements comparing each with eto hide it, hiding it if it is not equal e.

However, I remember that there is a jQuery function .filter(http://api.jquery.com/filter/), but it only matches the element, not the other way around. (i.e. if I call the function and pass it e, it will only match e, not all BUT e.)

Is there a way to do this, or are there recommendations for making the accordion as a whole? Thanks in advance!

+3
source share
1 answer

Well, then you should consider using a function .not().

var excludeTheseDOMElements = $('#something');
$('match_something').not(excludeTheseDOMElements).each(function(){
     //do stuff here
});

UPDATE:

You can also use a pseudo selector :not()-

$('match_something:not(#something)').each(function(){
    //do stuff here
});
+3
source

Source: https://habr.com/ru/post/1777522/


All Articles