Anand
Anand

Reputation: 1

Is it possible to combine two jquery functions for the same CSS selector?

I have a css selector in which it is toggling the default class display:none and show class display:block.

Is it possible to merge the below in a single query?

$('.abc .def').slideToggle();

$('.abc .def').toggleClass('show');

Upvotes: 0

Views: 88

Answers (2)

Shomz
Shomz

Reputation: 37701

There's no need to have a custom class just for toggling display because the slideToggle function is already modifying the display property inline.

If you still want to toggle the class as well, probably the best place is in the callback function, like this:

$('.abc .def').slideToggle(function(){
    $(this).toggleClass('show');
})

This will toggle the class once the slide animation is done.

Upvotes: 4

Mivaweb
Mivaweb

Reputation: 5722

Just add the 2 functions after each other, they always return the current object for chaining

$('.abc .def').slideToggle().toggleClass('show');

Upvotes: 2

Related Questions