Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Having an Issue Defining Variable Globally In jquery

I'm trying to keep what I save in local storage on the page, that way if I were to refresh I would still have whatever I entered in the textarea box.

However I'm not sure if it's because I am using a variable that assigns to e.target then the relative path to what I am trying to save, but when I console.log my variable it says it's not defined and I'm assuming that my issue is because I don't fully understand jQuery.

function updateHours() {}

function handleSave(e) {
  var value = $(e.target).siblings('.description').val()
  var hour = $(e.target).closest('.time-block').attr('id')
  localStorage.setItem(hour, value)
}

function main() {
  updateHours();
  $(document).on('click', '.saveBtn', handleSave)
  console.log(localStorage.getItem(value))
}

$(document).ready(main)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try something like this:

function updateHours(){
    document.querySelectorAll('.time-block').forEach(el => {
        const savedValue = localStorage.getItem(el.id);
        const description = el.querySelector('.description');
        if (savedValue && description) {
            console.debug("Load value", el.id, savedValue);
            description.value = savedValue;
        }
    });
}

function handleSave(e){
    const el = e.target.closest('.time-block');
    if (!el) { return; }
    
    const description = el.querySelector('.description');
    if (!description) { return; }
    
    console.debug("Save value", el.id, description.value);
    localStorage.setItem(el.id, description.value);
}

function main(){
    updateHours();
    
    document.addEventListener('click', e => {
        if (!e.target.classList.contains('saveBtn')) { return; }
        handleSave(e);
    });
}

window.addEventListener('DOMContentLoaded', main);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda