I want to send the option selected from the dropdown to local storage and have it show up on another page but it's empty


Nitin

This is the dropdown menu in the first html page with 1 option selected.

<body>
   <label for="select">Gender:</label>
   <select name="select" id="select">
     <option value = "0">Select</option>
     <option value = "1">Male</option>
     <option value = "2">Female</option>
     <option value = "3">Other</option>
   </select>
</body>

Here is the script I use to store the selected option in local storage.

<script>
    var options = [];
    $("#select option").filter(":selected").each(function(index) {
      var option = $(this).text();
      options.push(option);
      $("#select").text(options.toString())

    });

    localStorage.setItem('select', options);
</script>

This is another page where I want to display the selected option.

<body>
  <p> Gender: <span id="display_select"></span></p>
</body>

Here is the script to get the selected option from local storage and display it on the second page, showing empty.

$(document).ready(function() {

   for (let i = 0; i < localStorage.length; i++) {
     var options = localStorage.getItem("select");
     $("#display_select").text(options);
   }
});
Martinho

localStorage only supports strings. For this you have to use JSON.stringify() and JSON.parse().

localStorage.setItem('select', JSON.stringify(options));

//...
var storedSelect = JSON.parse(localStorage.getItem('select'));

Your example gets a select:

$('#select').change(function() {
    localStorage.setItem('select', $('#select option:selected').text());
});

var select = localStorage.getItem('select');
$("#display_select").html(select);  

Related


Show a dropdown when an option is selected in another dropdown

Sushma A: I have a main dropdown, when an option is selected in the dropdown, in the next text field, another dropdown must have a different value, and when another option is selected in the main dropdown, the text must start with the same way the text field a

Show a dropdown when an option is selected in another dropdown

Sushma A: I have a main dropdown, when an option is selected in the dropdown, in the next text field, another dropdown must have a different value, and when another option is selected in the main dropdown, the text must start with the same way the text field a

Show a dropdown when an option is selected in another dropdown

Sushma A: I have a main dropdown, when an option is selected in the dropdown, in the next text field, another dropdown must have a different value, and when another option is selected in the main dropdown, the text must start with the same way the text field a

Show selected dropdown option in new page

that guy I created a store in Rails, I created a product controller with name, category, description and image. When adding a product admin, my category is a dropdown selection option, from which the user can select the category the product belongs to. This is

Send selected dropdown value to another PHP page

user: I don't use submit button I want to send the value from the user selection to the test1.php page, but when the user selects an option, nothing happens on the page, and the value is not sent to the test1.php page Is there any bug in my code, or am I missi

Send selected dropdown value to another PHP page

user: I don't use submit button I want to send the value from the user selection to the test1.php page, but when the user selects an option, nothing happens on the page, and the value is not sent to the test1.php page Is there any bug in my code, or am I missi

Dropdown selected option select option in another dropdown

Matthew Moore I was looking for how to use it when I selected the Volvo option and then the 740. I want to add other if statements but I can't figure it out. If I choose Saab, I want it to choose 240 I want to use jQuery or JavaScript Thank you in advance. Thi

Dropdown selected option select option in another dropdown

Matthew Moore I was looking for how to use it when I selected the Volvo option and then the 740. I want to add other if statements but I can't figure it out. If I choose Saab, I want it to choose 240 I want to use jQuery or JavaScript Thank you in advance. Thi

Dropdown selected option select option in another dropdown

Matthew Moore I was looking for how to use it when I selected the Volvo option and then the 740. I want to add other if statements but I can't figure it out. If I choose Saab, I want it to choose 240 I want to use jQuery or JavaScript Thank you in advance. Thi