Notsogood13
Notsogood13

Reputation: 109

jquery toggle function open/close

Trying to do a function that does "A" when toggle open and "B" when toggle close.

Code:

function awesome_toggle()
{
    $(".button").click(function()
    {
        $("content").toggle(
            function()
            {
                /* Do "A" */
            },
            function()
            {
                /* Do "B" */
            }
        );
    });
}

This code doesn't seem to work and I can't figure out why. When I use only one function, so I stop the code after /* Do "A" */ it works but when I add the /* Do "B" */ function it stops working. What is wrong?

jquery version: ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js

Upvotes: 1

Views: 4137

Answers (1)

Zach Lysobey
Zach Lysobey

Reputation: 15714

As the comments have said, this version of toggle has been deprecated.

There may very well be a better way to accomplish what you want, but you can check whether the element is visible after toggle with the :visible selector, and do stuff conditionally based upon that.

function awesome_toggle()
{
    $(".button").click(function() {
        $("content").toggle(normalSpeed, function () {
            if ($("content").is(':visible')) {
                /* Do "A" */
            } else {
                /* Do "B" */
            }
        });
    });
}

Upvotes: 6

Related Questions