Use your left/right keys to browse tutorials
JQuery fading a div after a certain time

JQuery fading a div after a certain time

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 21, 2009

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.

demo


Recent shares

More tutorials from Papermashup
Comments
15 discussions around JQuery fading a div after a certain time
  1. Artbeard says:

    So few lines of code and yet so powerful, perfect!

  2. Eric says:

    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.

  3. Chris says:

    Is there anyway you can do this this multiple divs sequentially?
    EG:

    Content
    Content
    Content
    Content
    Content

  4. diego says:

    Hey that was great, very useful, thanks from Argentina… :)

  5. J says:

    Thank you worked without having to install any plugin!

  6. james says:

    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?

  7. Steven says:

    I was looking for a fade in and fade out tutorial and this is the real deal.

  8. chestaz says:

    thank Ashley ! im looking this thing for many time.. at the end your article save my day :)

  9. Kevin Rinker says:

    this is awesome. what if i want somthing to show after a time period?

    • Ashley says:

      @Kevin you just need to change fadeOut to fadeIn and remove this line $(“div.mydiv”).remove(); your div element will then fade in. :)

  10. mark says:

    Excellent job!

    EXACTLY what i needed.

  11. Karl says:

    Great tip, been looking for something like this for a long, long time.

    Good work :)

  12. Nice piece of information to get me started! Think it’ll come in handy when confirming, for example, a save when using AJAX.

  13. Hey cool this is like a behind the scenes of DDC lol I if I needed this I sure would use it lol

  14. Ashley says:

    Ha, yeah subscribe to my feed and get the inside gossip! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Looking for a registry cleaner to speed up your PC and show a full diagnostics?

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.