2

There is a pretty similar question here but I'm doing something different. In my case when submitting the form if something fails I'm doing a history.back() to keep all values in the form, but in the case of the input files the file name remains there but there is actually no file in there. So to avoid confusion and let the user know it needs to upload the file again I want to clear the value of that element only, and another input text that has the file description.

Adding autocomplete="off" attribute to it does the trick since prevent the browser to cache that value, but this works in Chrome but not in IE11.

I can also do it using JQuery like $input_file.val('').end(); but since I'm trying to do it after history.back() is not working not sure why.

<script>
      alert("Error");
      window.history.back();
      $('input[id="Attachment"]').val('').end();
</script>

Any other idea on how to accomplish this is welcome.

Somebody
  • 2,667
  • 14
  • 60
  • 100
  • Probably want to try this - https://stackoverflow.com/questions/8861181/clear-all-fields-in-a-form-upon-going-back-with-browser-back-button – user2884707bond Apr 03 '18 at 15:33
  • @user2884707bond that did the trick! Please add a response so I can give you the credit. – Somebody Apr 03 '18 at 15:45

1 Answers1

1

There is a trigger called pageShow that gets fired when the transition animation has completed.

jQuery( ".selector" ).on( "pageshow", function( event ) { ... } )

In your case, you'd want to do the following

<script>
  alert("Error");
  window.history.back();
  $(window).bind("pageshow", function() {
      //debugger; you can check and see if this block of code gets triggered 
      $('input[id="Attachment"]').val('').end();
   });
</script>

Note: The triggering of this event is deprecated as of jQuery Mobile 1.4.0. It will no longer be triggered in 1.6.0. The replacement for pageshow is the pagecontainer widget's pagecontainershow event. In jQuery Mobile 1.4.0, the two events are identical except for their name and the fact that pagecontainershow is triggered on the pagecontainer, whereas pageshow is triggered on the page.

jQuery Reference to the event

examples

user2884707bond
  • 559
  • 4
  • 24