Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform Javascript action after reset button handles click

How do I perform an action immediately after an <input type="reset"/> has already reset the form elements?

like image 710
user541686 Avatar asked Jul 18 '11 20:07

user541686


People also ask

How do I reset a form in JavaScript?

There are two buttons that are "Submit", and "Reset data". When we click the Reset data button, it calls the function fun (), where we have defined the JavaScript's reset () method. In the function fun (), we are first taking the reference of the form required to reset, and then we are applying the reset () method over it.

How to handle back button functionality in JavaScript?

For instance, navigation can be done using keyboard keys and refresh can also be done using F5 or CTRL+R that cannot be handled using the code above. In order to handle back button functionality, we need to come up with a solution that requires server-side effort together with client-side JavaScript code. The concept is...

How do I execute a JavaScript function from a button?

You place the JavaScript function you want to execute inside the opening tag of the button. Note that the onclick attribute is purely JavaScript. The value it takes, which is the function you want to execute, says it all, as it is invoked right within the opening tag.

How do you use onClick event in JavaScript?

How to Use the onclick event in JavaScript The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you change certain content on the web page, and other things like that. You place the JavaScript function you want to execute inside the opening tag of the button.


3 Answers

Try :

<input type="reset" onclick="return resetForm();"/>

function resetForm(){
    setTimeout(function(){
        // do whatever
    }, 50);
    return true;
}
like image 65
ChristopheCVB Avatar answered Oct 28 '22 04:10

ChristopheCVB


Forms have a reset event that you can listen for.

<script>
function resetHandler() {
    // code
}
</script>
<form ... onreset="resetHandler();">
</form>

Of course, it's bad practice to add javascript handlers this way, so you'd want to use .addEventListener/.attachEvent or jQuery.bind(), but you get the idea.

like image 37
digitalbath Avatar answered Oct 28 '22 04:10

digitalbath


Write code/events which you wanted to call in middle of this function. I have tested this. Working good.

$(document).ready(function() {
    $("input:reset").click(function() {       // apply to reset button's click event
        this.form.reset();                    // reset the form

        // call your functions to be executed after the reset      

         return false;                         // prevent reset button from resetting again
    });
});
like image 44
Somnath Muluk Avatar answered Oct 28 '22 03:10

Somnath Muluk