vineet
vineet

Reputation: 14236

Multiple functions in jquery toggle

I have seen multiple functions inside jquery toggle function but i cannot understand their flow of execution. Even there is no any documentation in jquery Official site.

Here is function:-

 $('#menu-toggle').toggle(
    function() {
        console.log('toggle 1 fn');
        $('body').addClass('left-side-collapsed').removeClass('sidebar-colors');
        $('#sidebar .slimScrollDiv').css('overflow', 'initial');
        $('#sidebar .menu-scroll').css('overflow', 'initial');
    }, function() {
        console.log('toggle 2 fn');
        $('body').removeClass('left-side-collapsed')
        $('#sidebar .slimScrollDiv').css('overflow', 'hidden');
        $('#sidebar .menu-scroll').css('overflow', 'hidden');
    }
);

Upvotes: 2

Views: 578

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The toggle() method used to take multiple parameters, each a function, which would be executed in turn on click of the element.

Since jQuery 1.8 this pattern has now been deprecated and as of 1.9 it was removed from the source entirely. To replicate it you would need to check what class is currently on the element and toggle it yourself, something like this:

$('#menu-toggle').click(function() {
    if ($('body').hasClass('left-side-collapsed')) {
        $('body').removeClass('left-side-collapsed')
        $('#sidebar .slimScrollDiv, #sidebar .menu-scroll').css('overflow', 'hidden');
    } else {
        $('body').addClass('left-side-collapsed').removeClass('sidebar-colors');
        $('#sidebar .slimScrollDiv, #sidebar .menu-scroll').css('overflow', 'initial');
    }
});

Alternatively you could add the previous implementation of toggle() back in to jQuery yourself, using the original source code. Note, however, that this is untested and outdated and may interfere with other methods, or rely on methods that no longer exist.

Upvotes: 2

Related Questions