Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
Persistent selected option on page reload

I am trying to persist a selected option in a select element on the page reload. My select gets populated dynamically and once an item get selected I need it to append the value of the option to the URL and trigger a window.location to reload the page.

I am trying to do it with jQuery but would not be opposed to do only with plain JS if that's possible. This is what I've tried but the select option keeps getting back to the very first one on every page reload.

Does anyone have an idea what might be wrong here? Thank you!

$(function(){
    var storedValue = localStorage.getItem('selectedProject');
    if(storedValue != null){
        $("#projectsList").first().find(":selected").removeAttr("selected");        
        $("#projectsList").find("option").each(function () {
                if ($(this).val() == storedValue) {
                    $(this).attr("selected", true);
                }
            });
    }
    $('#projectsList').change(function () {
        localStorage.setItem("selectedProject", $("#projectsList").first().val());
        window.location = window.location.href + "?project=" + localStorage.getItem('selectedProject');
    });

    $('#projectsList').change(function () {
         var currValue = $(this).val();
         localStorage.setItem('selectedProject', currValue );        
    })
    // set stored value when page loads
    .val(storedValue)

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="projectsList" class="selectpicker show-tick project-list" data-show-subtext="true" data-live-search="true" data-width="35%">
   <option value="value1" data-tokens="value1">value1</option>
   <option value="value2" data-tokens="value2">value2</option>
   <option value="value3" data-tokens="value3">value3</option>
</select>

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You have two .change handlers, and the one that reloads the page, always sets the localstorage to the first item option in your list.

Just using a single .change handler should solve your problems.

  $("#projectsList").change(function () {
    var currValue = $(this).val();
    localStorage.setItem("selectedProject", currValue);
    window.location =
      window.location.href + "?project=" + localStorage.getItem("selectedProject");
  }).val(storedValue);
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!