Catching more than one element with JQuery / JavaScript

Posted on

Question :

Good morning!

Would you like me to get more than one element with JQuery?

For example, I’m doing this in my code:

if (!is992()) {
        $j('.filters .title--sm').click(function(){
        $j('.filters .title').toggleClass('active'); 

I wanted to optimize these two lines:


Would you like me to take both classes and use it in single lines? For example:

$j('.filters__list', '.filters__filtered').slideToggle(50);

So I know you do not, but I wanted to know if there’s something I can use.

Thank you!


Answer :

You can use the comma to concatenate selectors in CSS. You were close, it would look like this:

$j('.filters__list, .filters__filtered').slideToggle(50);

If you have many classes to join you might want to give a common class to all those elements.


Leave a Reply

Your email address will not be published. Required fields are marked *