Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset form on Back button

Trying to find an elegant solution to reset a webform (or more specifically two INPUT Type='hidden' fields) if a user goes BACK after the form is submitted.

I understand that BACK button does not trigger an onload event.

But is there any way of dealing with this?

like image 837
Uno Mein Ame Avatar asked Feb 18 '26 05:02

Uno Mein Ame


1 Answers

when the back button is pressed, onbeforeunload event is fired so you can try something like

jQuery:

$('#form_id').submit(function(){
$(window).unload(function () {
$('input[type="hidden"]').val("reset_value"); 
}
});

javascript:

<form onsubmit="call_function();">

in between script tag:

function call_function(){

window.onbeforeunload = function(){
var input_elems = document.getElementByTagName('input');
for(var i=0;i<input_elems.length;i++){
     if(input_elems[i].type === 'hidden'){
         input_elems[i].innerHTML = "reset_value";
     }
   }
}
like image 146
dku.rajkumar Avatar answered Feb 20 '26 17:02

dku.rajkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!