2

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');
    }
);
2

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.