On websites such as eBay, upon returning to a search page, the search options are all filled in. I'd like to do the same with my website, where currently, returning to the search page returns the correct search results but the search form is empty.
The two approaches I've seen to this so far:
- Have the form written in HTML, use JS to parse query string and set form values.
How can I pre-populate html form input fields from url parameters?
- Generate the form in JS with any values in the query string, this will make a blank form if there's no query string given.
Query String for pre-filling html form field
Both these solutions seem hack-y. I'd imagine that this is a common task, is there a standard or clean method to do this in one or two lines?
There is obviously something that serializes the fields from a form to a query string, there should be a way to use that in reverse to take the query string and fill in the form fields, without having to "roll your own" loop in JS.