b11 b11 - 1 month ago 4
Javascript Question

How to reload page and update selection when a new value is selected in JQuery

I have a select box. On page load, "5" is selected, when I select "1" from the list, I want to reload the page and have 1 be selected on default. How do I accomplish this in JQuery.

There are
<select class="num_apples form-control">
<option value="1">1</option>
<option value="5" selected="selected">5
</option><option value="50">50</option>
apples on the table

I tried doing this in JQuery

$(".num_apples").change (function() {
$(this).val = $(".num_apples").selected;


Save the selected value and check on page load if a value is saved and set the value of the select accordingly:

var selected = localStorage.getItem('selected');
if (selected) {

$(".num_apples").change(function() {
  localStorage.setItem('selected', $(this).val());