Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda