Reputation:
Can somebody explain me how to reset the form when I reload the page?
I tried things like:
$(document).ready(function() {
$('#contact-form')[0].reset();
});
and:
<body id="final-example" onload="document.my_form.reset();">
It works but then everytime I reload or go to the another page I get this error (f12) :
"TypeError: document.my_form is undefined"
I guess the problem is that I try to clear the form on every page beacause of the onload and $("document").ready() but there is no form on every page except one.
I must say that I included header on every page with php include and my header contains body opening tag so maybe because of that it tries to execute that reset on every page?
So can I avoid this somehow so that it like works only for that one page when I reload it or when I go back to it from another page? Or should I maybe remove the php include (header) function on that page and just paste the code? Thanks a lot.
Upvotes: 0
Views: 5257
Reputation: 5246
Please try with below. And remove this document.my_form.reset();
from body onload event.
$(document).ready(function() {
//This condition will check if form with id 'contact-form' is exist then only form reset code will execute.
if($('#contact-form').length>0){
$('#contact-form')[0].reset();
}
});
Upvotes: 2