0

I'm trying to reset the form when the user click on the back button. Here is the code that I use.

<input type="text" name="name">
<input type="text" name="email>
...
$(function() {
   $('input[type=text]').attr('val', '');
});

This code is not working on Safari 6.0.2. I notice that the page load event is not even fired when the user click on the back button.

What could be the problem ?

robosot
  • 51
  • 3
  • There's no such attribute called `val`. And getting something to fire on back button reliably cross-browser is going to be a real challenge. Usually, there is no page `load` on `back`. – Sparky Nov 18 '12 at 16:24
  • It looks like he's just resetting it on document ready, which would be called when the back button is pressed. – Charlie Nov 18 '12 at 16:25
  • Does this help: http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button/170478#170478 – Tchoupi Nov 18 '12 at 16:26
  • @Charlie, [it's really not that straightforward](http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button). – Sparky Nov 18 '12 at 16:29
  • @Mathieu Imbert, thanks but not working for me. – robosot Nov 18 '12 at 16:35
  • robosot, read the link in my last comment above. – Sparky Nov 18 '12 at 16:41

1 Answers1

2

There's no val attribute. Use the val method instead:

$(function() {
   $('input[type=text]').val('');
});

And, you missed a quote at the email field:

<input type="text" name="email>
                              ^ Insert a quote here
Sock Puppet
  • 120
  • 6
  • The code in the answer is fine... too bad `document.ready` does not fire when the page is reached via the back button. – Sparky Nov 18 '12 at 16:43
  • @robosot Another way to achieve empty fields is to empty the fields on unload. Does that help? – Sock Puppet Nov 18 '12 at 17:00
  • I tried with the unload. The event fires but when I click back button the fields are not reset. – robosot Nov 18 '12 at 17:14