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

218
Vistas
How to load localStorage value using fetch() on page load?

I have 2 html pages, index.html and dropdown-component.html

dropdown-component.html:

<select id="AssistSelectMenu">
  <option value="assist/AK.html">Alaska</option>
  <option value="assist/AL.html">Alabama</option>
  <option value="assist/AZ.html">Arizona</option>
</select>

onclick of a button in index.html, I try to fetch the content of dropdown-component.html

Using the ff:

function fetchJXSelectMenu() {
fetch('dropdown-component.html')
    .then(response => response.text())
    .then(text => document.querySelector('#component-assist-jx-select-menu').innerHTML = text);
}

It is loaded in a div <div id="component-assist-jx-select-menu"></div>.

The problem is, the "component" it loads is a select/option that utilizes localStorage to remember the last selected option of the user.

It looks like this:

$(function () {
    $('#AssistSelectMenu').change(function () {
        localStorage.setItem('JXselect', this.value);
    });
    if (localStorage.getItem('JXselect')) {
        $('#AssistSelectMenu').val(localStorage.getItem('JXselect'));
    }
});

If I'm not using the fetch() method, the localStorage function works fine, but if I do use it, it fails to load the last selected option of the user on page load. I'm guessing this is because the browser has already finished rendering the page and can't process the localStorage for the newly loaded component.

So my question is how do I workaround this? I tried putting the script in the component but that didn't solve it at all.

Thank you in advance for any help!

about 4 years ago · Juan Pablo Isaza
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