Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function after form reset

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.

like image 286
Virgil Balibanu Avatar asked Nov 16 '11 14:11

Virgil Balibanu


People also ask

How do you call a function again and again?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.

Which event occurs when the reset button in a form is clicked?

The onreset event occurs when a form is reset.

How do you reset inputs on a form?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

What does reset () do in JavaScript?

reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method. It does not reset other attributes in the input, such as disabled .


1 Answers

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
    });
});
like image 95
vol7ron Avatar answered Oct 20 '22 04:10

vol7ron