Something as simple as:
$("#div").addClass("error").delay(1000).removeClass("error");
doesn't seem to work. What would be the easiest alternative?
Something as simple as:
$("#div").addClass("error").delay(1000).removeClass("error");
doesn't seem to work. What would be the easiest alternative?
Try this:
Delay operates on a queue. and as far as i know css manipulation (other than through animate) is not queued.
I know this this is a very old post but I've combined a few of the answers into a jQuery wrapper function that supports chaining. Hope it benefits someone:
And here's a removeClass wrapper:
Now you can do stuff like this - wait 1sec, add
.error
, wait 3secs, remove.error
:$('#div').delay(1000).queueAddClass('error').delay(2000).queueRemoveClass('error');
jQuery's CSS manipulation isn't queued, but you can make it executed inside the 'fx' queue by doing:
Quite same thing as calling setTimeout but uses jQuery's queue mecanism instead.
You can create a new queue item to do your removing of the class:
Or using the dequeue method:
The reason you need to call
next
ordequeue
is to let jQuery know that you are done with this queued item and that it should move on to the next one.Of course it would be more simple if you extend jQuery like this:
after that you can use this function like addClass: