tags:

views:

23

answers:

2

I'm inserting an h3 tag when a form is submitted. How do i fade that h3 tag out in 5 seconds using jquery?

I tried this but it didn't work. Do i have to use .live?

$(document).ready(function() {
    $('h3').delay(5000).fadeout(300);
});

EDIT: I'm submitting the form to itself in case that matters.

+1  A: 

I think in your situation, you would just want to do this in your form submit:

    //insert your h3
    var header = $('<h3>My Header!</h3>');
    //append to wherever
    $('body').append(header);
    //set a timeout, then fade away!
    setTimeout(function() {
        header.fadeOut(300);
    }, 5000);

Here's a demo of that: http://jsfiddle.net/wSXfw/

Ender
do i really have to use a setTimeout?
Catfish
You don't have to, but it's the best solution. What's your hesitation with that?
Ender
Well nothing really, i just thought there would be a better way.
Catfish
except i'm not appending any html via jquery, i'm setting the message using php so your solution does not work.
Catfish
You can use jQuery's .delay() as you were doing in your question, and that will also work. However, the documentation for the function explicitly notes that it's not meant to replace setTimeout(). Mostly it comes down to how you want to do it.
Ender
actually it works. I had a jquery error. Thanks alot man!
Catfish
Happy to help out.
Ender
A: 

You are saying that you submit the form, the page reloads, and you print an h3 tag to the page from the server? Then you want to fade that h3 out quickly after it has been on the page for five seconds?

$(function () { $("#the-h3").delay(5000).fadeOut(300); }

Could it be that you did not camelcase the fadeOut() ?

tandu
That could very well be the reason actually.
Catfish