11

Is there a method for me to call a function after click on the reset button in form, and I mean after, so that the form is first reset and then my function called. Normal event bubbling would call my function and only then reset the form. Now I would like to avoid setTimeout in order to do this.

What I need is to call a function when a form is reset because I use uniform and uniform needs to be updated when values change.

At the moment I do it like this:

//Reset inputs in a form when reset button is hit  
$("button[type='reset']").live('click', function(){  
    elem = this;  
    //Sadly we need to use setTimeout to execute this after the reset has taken place  
    setTimeout(function(){  
        $.each($(elem).parents('form').find(":input"), function(){  
            $.uniform.update($(this));  
        });  
    }, 50);  
});  

I tried to do al this on $(':input').change() but reseting an element does not seem to trigger the change event.
Thank you in advance for any help.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Virgil Balibanu
  • 1,037
  • 4
  • 11
  • 16

5 Answers5

7

I haven't yet tested in all browsers, but you can do your own ordering within a click event: http://jsfiddle.net/vol7ron/9KCNL/1/

$(document).ready(function() {
    $("input:reset").click(function() {       // apply to reset button's click event
        this.form.reset();                    // reset the form
        window.alert($("input:text").val());  // call your function after the reset      
        return false;                         // prevent reset button from resetting again
    });
});
vol7ron
  • 40,809
  • 21
  • 119
  • 172
7

HTML forms do have an onReset event, you can add your call inside there:

function updateForm()
{
    $.each($('form').find(":input"), function(){  
        $.uniform.update($(this));  
    });  
}

<form onReset="updateForm();">

As pointed out in the comment by Frédéric Hamidi you can also use bind like so:

$('form').bind('reset', function() {
    $.each($(this).find(":input"), function(){  
        $.uniform.update($(this));  
    }); 
});

After some testing it appears both ways fire before the reset takes place and not after. The way your doing it now appears to be the best way.

The same conclusion was found in this question here

Community
  • 1
  • 1
Jeff Wilbert
  • 4,400
  • 1
  • 20
  • 35
  • 1
    On my browser (Firefox 8), binding to `reset` still runs the handler before the fields are actually reset (fiddle [here](http://jsfiddle.net/9KCNL/)). This behavior is probably required to support `preventDefault()` on the `reset` event. – Frédéric Hamidi Nov 16 '11 at 14:14
  • True, you can also bind to `reset`. Doesn't appear either way fires after the reset is done and doubt there's any real efficient way to do it besides using a timeout like he's doing now. – Jeff Wilbert Nov 16 '11 at 14:20
4

Time ago I worked debugging a Google IE related plugin and I solved the main error with a bubbling trick. That's why I think immediately in this solution for your problem (of course should be cross-browser):

<form>
    <div id="capture_bubble">
        <input type="text"><input type="reset">
    </div>
</form>

In this way you can capture the bubbling with $('#capture_bubble') after reset event be triggered.

You can make a quick test with:

(function($) {
    $(function() {
        $('#capture_bubble').live('click', function(){
            console.debug('capture_bubble');
            alert('capture_bubble')
        })
        $("input[type='reset']").live('click', function(){
            this.form.reset(); // forcing reset event
            console.debug('reset');
            alert('reset')
        });                 
    });
})(jQuery);

Please note: this.form.reset(); (change made due to a jeff-wilbert observation)

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • 3
    While in theory that solution sounds good and by looking at the console log the bubbling effect does work in that reset gets called first and logged followed by the capture_bubble but the form doesn't actually reset right when the reset button is clicked and the values still remain when inside the capture_bubble, I created a fiddle here testing this: http://jsfiddle.net/Awfap/ basically I just added an alert inside the capture_bubble event to read the value of an input box in the form only after reset has been clicked by using a boolean variable. – Jeff Wilbert Nov 16 '11 at 16:02
  • 1
    @jeff-wilbert this jsfiddle tool is great!. Please check [http://jsfiddle.net/Awfap/3/](http://jsfiddle.net/Awfap/3/) I just forced a reset event. What do you think? – Igor Parra Nov 16 '11 at 16:56
  • 1
    Yeah jsfiddle is awesome; I just found out about it myself a couple days ago being new here myself. It's great for testing and creating working demos for people. That being said your new change of forcing the reset seems to work awesome and in every browser so, very nice. +1 to your answer – Jeff Wilbert Nov 16 '11 at 19:47
  • @jeff-wilbert +1 thanks. good observations and a good tool presented. – Igor Parra Nov 16 '11 at 21:08
  • If the reset is forced. Do you need bubbling? – Beanow Sep 03 '12 at 12:39
3

you shouldn't need to wait 50 milliseconds. If you use setTimeout with a timeout of zero, it effectively means "push this code onto the event stack". Since the form-reset is guaranteed to have fired first, the code in the setTimeout is guaranteed (in well behaved javascript interpreters) to have access to the form values you want (post-reset). You should be able to use the code below, guilt-free.

var afterReset = function(){
    var pushMeOntoTheEventStack = window.setTimeout(function(){
        $("#form input").each(function(){
            console.log( this.name + ' = ' + this.value );
        });
    },0);
};
$("#form").on("reset",afterReset);
code_monk
  • 9,451
  • 2
  • 42
  • 41
  • see [window.postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) for a more modern way to do this – code_monk Feb 28 '17 at 10:23
  • I just encountered this problem.The reset event fires before resetting the values. I would like to know more about this. Any idea where i could read about this peculiar behaviour? – dsasko Sep 25 '17 at 19:38
  • this is a great reference: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events – code_monk Sep 26 '17 at 14:07
0

Try this solution

Goal:

  • add on "click" event
  • prevent the default action (reset)
  • trigger "reset"
  • run desired code

Example:

$("button[type='reset']").on('click', function(evt) {
   evt.preventDefault();
   $(evt.target).trigger('reset');

   // enter code to run after reset
   $.each($(this).find(":input"), function(){  
      $.uniform.update($(this));  
   }); 

});
propjk007
  • 655
  • 1
  • 10
  • 18