Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop the jQuery autocomplete plugin from forgetting text when the user clicks back

I'm using the jQuery autocomplete plugin to get a list of locations, which works fine. But if the user clicks the browser's back button after submitting the page with the autocomplete textbox, the textbox is empty. If I take the autocomplete off the textbox and submit & click back it remembers the text.

Is there a way to stop the autocomplete from clearing the textbox when the page loads?

like image 640
Glenn Slaven Avatar asked Jan 14 '09 00:01

Glenn Slaven


3 Answers

I had the same problem today, and found a way around this.

$("#search_form").submit(function() {
    $("#location")[0].removeAttribute("autocomplete");
});

This code will remove the autocomplete attribute just before the form is submitted. You will have to change the selectors so that they match your form and input(s).

like image 75
heyman Avatar answered Nov 20 '22 14:11

heyman


Well I found the issue. It seems to be on Firefox, not IE, and it's not technically due to the autocomplete plugin. It's because the plugin adds the attribute autocomplete="off" to the textbox.

This is so that the browser's autocomplete history doesn't conflict with jquery's autocomplete, but in Firefox, fields that have this attribute don't get pre-populated when the user clicks the back button.

I'm guessing there isn't a way around this, it appears to be the default browser behaviour. If anyone knows different please post a comment.

like image 45
Glenn Slaven Avatar answered Nov 20 '22 12:11

Glenn Slaven


in the $.autocompleter function on the plugin there is a variable called previousValue at line 72 of the unpacked version of the jquery.autocomplete.js. This variable is set to an empty string on initialization.

If you're feeling ambitious, you could try editing this script and have if check if the textbox already has a value, and set previousValue to it if it does.

like image 2
Soviut Avatar answered Nov 20 '22 13:11

Soviut