Moved autocomplete to a function

Autocomplete stops working when I move between two pages, strange.