Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle() with no animation but complete function?

Tags:

jquery

I'm trying to toggle an element's visibility without animation, but with a complete function so that I change the text of the link initiating the toggle.

jQuery('.toggle_tags').click(function(){
    var elem = jQuery(this);
    jQuery('#taglist').toggle({complete: function() {
        if (jQuery(this).is(':visible')) {
            elem.text('(Hide Tags)');
        } else {
            elem.text('(View Tags)');
        }
    },});
    return false;
});

Unfortunately this is still triggering animation. Without ANY params, toggle doesn't animate but even either by passing just a function or the complete function verbosely (as above), I still get animation.

Any advice?

like image 570
deed02392 Avatar asked Dec 16 '22 11:12

deed02392


1 Answers

Don't use a complete function.

Without parameters, .toggle() will operate synchronously, meaning that you can just test what it did in the next statement, knowing that it will already have finished, you could use:

jQuery('#taglist').toggle();
if (jQuery('#taglist').is(':visible')) {
    elem.text('(Hide Tags)');
} else {
    elem.text('(View Tags)');
}
like image 165
Alnitak Avatar answered Jan 15 '23 04:01

Alnitak