javascriptjquerycallbackslidetoggle

jQuery slideToggle() callback function


Here is the jQuery slideToggle function:

$('.class').click(function() {
    $(this).parent().next().slideToggle('slow', function() {
        // how can I access $('.class') that was clicked on
        // $(this) returns the $(this).parent().next() which is the element
        // that is currently being toggled/slided
    });
});

In the callback function I need to access current .class element (the one being clicked on). How can I do that?


Solution

  • Take a reference to the element outside of the callback, you can then use this inside the callback function.

    $('.class').click(function() {
        var $el = $(this);
        $el.parent().next().slideToggle('slow', function() {
             //use $el here
        });
    });