I want to use a select to change the query on the end of a url to change it’s sort options. e.g:
<select id="sort">
<option value="?order_by=date">Recent</option>
<option value="?order_by=random">Popular</option>
<option value="?order_by=random">Random</option>
<option value="">Staff Picks</option>
</select>
so for example by default a list of posts will be shown by date and then if a user chooses an option it will reload the page with the query string on the end of the URL. If possible looking to use jQuery to achieve this. Thanks.
Attach a handler to the change event for the select box that adds the value of the selected option to the current window location with everything after the ? snipped off:
Here’s an example (it doesn’t redirect, but you get the idea…)
To have the appropriate value selected on page load, you can run the following function before you bind the change handler:
I’m not quite sure why you aren’t just using something like:
And, for the JS, if any, just:
Then, you don’t require anyone to have JavaScript enabled.
Like this?
Add
to your
<select>
.You might want to put a blank option at the top too.
Try that.