Here’s a handy bit of code that I use quite regularly, its particularly useful if your getting a response message from a form to notify the user that there data was sent, or comunicate an error and you want to fade the message out after a set time.
Firstly you need to include the jQuery framework you can download the latest version here.
All you need to change is the target div, in our case ‘mydiv’
$(document).ready(function(){
setTimeout(function(){
$("div.mydiv").fadeOut("slow", function () {
$("div.mydiv").remove();
});
}, 2000);
});
To adjust the amount of time until the fade function is triggered simply change the ’2000′ to a different number, currently set at 2 seconds or 2000 milliseconds.
So few lines of code and yet so powerful, perfect!
Nice job with this. I’m using this and it works fine by itself, but when I add in the Jquery date picker (in a separate div altogether) it no longer fades and disappears. The date picker will work fine but not the div specified in this script.
Is there anyway you can do this this multiple divs sequentially?
EG:
Content
Content
Content
Content
Content
Hey that was great, very useful, thanks from Argentina…
Thank you worked without having to install any plugin!
This is great, thanks!!!
Question, in your demo it appears the div below the fade div jumps up after the fadeout – is there a way to make the div below slide up smoothly?
I was looking for a fade in and fade out tutorial and this is the real deal.
thank Ashley ! im looking this thing for many time.. at the end your article save my day
Excellent job!
EXACTLY what i needed.
Great tip, been looking for something like this for a long, long time.
Good work
Nice piece of information to get me started! Think it’ll come in handy when confirming, for example, a save when using AJAX.
Hey cool this is like a behind the scenes of DDC lol I if I needed this I sure would use it lol
Ha, yeah subscribe to my feed and get the inside gossip!
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
15 discussions around JQuery fading a div after a certain time