jquery - how to add/remove class on slideToggle?

41,047

You can toggle the class using .toggleClass() based on if the element .is() :visible after the animation, like this:

$('#btCategoriaA').click(function() {
  $('#listaCategoriaA').slideToggle('slow', function() {
    $('#btCategoriaA').toggleClass('selecionado', $(this).is(':visible'));
  });
});
Share:
41,047
MEM
Author by

MEM

Updated on November 01, 2020

Comments

  • MEM
    MEM over 3 years

    I would like to each time the div expands, to add the class 'selecionado' once the expansion is finished. When the contraction is done (the slideUp part) I would like to remove this class.

    Can I have a help here please?

    $('#btCategoriaA').click(function()
    {
      $('#listaCategoriaA').slideToggle('slow', function() {
       $('#btCategoriaA').addClass('selecionado');
      });
    });
    

    Thanks in advance, MEM