How do I perform an action immediately after an <input type="reset"/>
has already reset the form elements?
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.
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...
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 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.
Try :
<input type="reset" onclick="return resetForm();"/>
function resetForm(){
setTimeout(function(){
// do whatever
}, 50);
return true;
}
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.
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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With