9

I'm using a form and jQuery to make a quick change on a web site. I would like to change the button text to 'Saved!' then change it back to Update after a few seconds so the user can change the value again. Of course they can hit the now 'Saved!' button again, but it doesn't look nice.

$("form.stock").submit(function(){
    // Example Post
    $.post($(this).attr('action'), { id: '123', stock: '1' });
    $(this).find(":submit").attr('value','Saved!');
    // This doesn't work, but is what I would like to do
    setTimeout($(this).find(":submit").attr('value','Update'), 2000);
    return false;
});

5 Answers 5

14

First argument to setTimeout is function. So wrap your code inside an anonymous function and you are good to go.

$("form.stock").submit(function(){
    // Example Post
    $.post($(this).attr('action'), { id: '123', stock: '1' });
    var submit = $(this).find(":submit").attr('value','Saved!'); //Creating closure for setTimeout function. 
    setTimeout(function() { $(submit).attr('value','Update') }, 2000);
    return false;
});

I am not able to test this code right now. Let me know if it doesn't work.

EDIT: As suggested by redsquare, it makes sense to create closure from the submit button itself.

Sign up to request clarification or add additional context in comments.

1 Comment

no need to run the submit selector twice, you can put that in a var to use within the setTimeout
5

If it's actually a "button" tag then to change it you would use.

$('#button_id').html('New Text');

Of course if you're preforming this onClick for that button then you could easily just pass (this) into the function as an object and $(this).html('New Text'); instead.

Hope this helps someone.

Comments

2

I would suggest, perhaps, a different (in my opinion better) interface to give feedback than changing the text of the button. You could use jGrowl or the dialog widget to display a message via a callback from the post method.

$("form.stock").submit(function(){
    $.post(
        $(this).attr('action'),
        { id: '123', stock: '1' },
        function() { $.jGrowl("Your update was successfully saved."); }
    );
});

Comments

0

You probably want to wrap the action in a function:

setTimeout(function(){$(this).find(":submit").attr('value', 'Update')}, 2000);

1 Comment

I think 'this' inside the setTimeout anonymous function won't work correctly. One needs to use closure to capture 'this'.
0
$("form.stock").submit(function(){
    var $form = $(this);
    $.post( $form.attr('action'), { id: '123', stock: '1' } );
    var $submit = $form.find(":submit").attr('value','Saved!');
    window.setTimeout(function() {
         $submit.attr('value','Update') 
    }, 2000);
    return false;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.