Picking up more than one element with Jquery/Javascript

Asked

Viewed 263 times

3

Good morning!

Could I 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__list').slideToggle(50);
        $j('.filters__filtered').slideToggle(50);
        $j('.filters .title').toggleClass('active'); 
    });
}

I wanted to optimize these two lines:

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

Could I take both classes and use one line? For example:

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

So I know you can’t, but I was wondering if there’s anything like that I could use.

Thank you!

  • 1

    yes, only the formatting that is not very good use like this: $j('. filters_list . filters_filtered'). slideToggle(50);

1 answer

4


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

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

If you have many classes to join maybe it is better to give a common class to all these elements.

  • 2

    Ah, use the same in CSS? What a lack of attention to mine.

  • 1

    @Exact value. jQuery accepts CSS selectors and a few jQuery-specific selectors

  • 1

    Sergio, thanks! kkk And here I am already thinking of creating array. When the time comes, mark as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.