0

On clicking of a button, I want three things to happen, a slideToggle, toggle between 2 classes, and a setting of a cookie. I have the first two things working properly, I now need to set a cookie of companyDescriptionDiv to be either opened or closed depending on whether #showhide-company-description has a class of opened or closed

$(document).on('click', "#showhide-company-description", function () {
    $("#companydescription").slideToggle("slow", function () {
        $("#showhide-company-description").toggleClass("opened").toggleClass("closed");
    });
});
Claire
  • 3,683
  • 13
  • 47
  • 74

2 Answers2

1

Btw you don't have to call toggleClass twice, you can pass a comma separated list of classes to toggle, e.g. toggleClass('opened, closed'). Personally I'd do it like this:

$(document).on('click', "#showhide-company-description", function () {
    var $toggler = $(this);
    $("#companydescription").slideToggle("slow", function () {
        var isOpen = $toggler.toggleClass('opened, closed').hasClass('opened');
        if(isOpen) {
            setCookie.routine();
        });
    });
});

This will execute the toggle and return its "state" at the same time.

Simon
  • 7,182
  • 2
  • 26
  • 42
0

Check the class:

if ($("#showhide-company-description").hasClass("opened")) {
  //set cookie
}

Set cookie

Community
  • 1
  • 1
Pavel Staselun
  • 1,970
  • 1
  • 12
  • 24
  • I didn't want to do an if, I was hoping there was some way to to intercept the setting of the classes, e.g. ....toggleClass("opened",function(){) – Claire Jul 30 '12 at 09:46