jQuery slideToggle() callback function

Richard Knop picture Richard Knop · Aug 7, 2009 · Viewed 15.8k times · Source

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?

Answer

redsquare picture redsquare · Aug 7, 2009

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);
    $(this).parent().next().slideToggle('slow', function() {
         //use $el here
    });
});