Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery add class for certain time

Tags:

jquery

How I can add a css class to an element for only 10 seconds ?

like image 512
astropanic Avatar asked Feb 21 '11 14:02

astropanic


2 Answers

A nicely reusable way would be this little jQuery plugin:

(function($){

    $.fn.extend({ 

        addTemporaryClass: function(className, duration) {
            var elements = this;
            setTimeout(function() {
                elements.removeClass(className);
            }, duration);

            return this.each(function() {
                $(this).addClass(className);
            });
        }
    });

})(jQuery);

Use like so:

$("#myElement").addTemporaryClass("myClass", 10000);
like image 148
Dan S Avatar answered Oct 21 '22 20:10

Dan S


You can add the class, then call setTimeout(function() { ... }, 10000) to remove it 10,000 milliseconds later.

like image 44
SLaks Avatar answered Oct 21 '22 21:10

SLaks