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

153
Visualizações
Setting value of forms from a cookie on page load, but the value disappears as soon as I click on an input

I am trying to save form data as a cookie and then fill out a form if the user has a matching cookie on page load. It seems to work as intended, but then when I click on any input in the form, it causes the values that were set by javascript to clear.

My JS:

        // when the user click on the hs-submit button, serialize the form data and save it in a cookie
$('.hs-submit').click(function(e){
    e.preventDefault();
    var formID = $(this).attr('data-form');
    var form = $(`.form-${formID}`).find('form');
    var formData = form.serializeArray();
    // convert the form to a string
    var data = JSON.stringify(formData);
    $.cookie(`hs-form-${formID}`, data, { expires: 7, path: '/' });
});

// when the user loads the page, if they have a cookie for the form, populate the form
if($.cookie('hs-form-1')){
    var formData = $.cookie('hs-form-1');
    // convert the formData to parsedData
    var parsedData = JSON.parse(formData);
    var form = $(`.form-1`).find('form');
    $.each(parsedData, function(i, field){
        form.find(`[name="${field.name}"]`).val(field.value);
    });
}
about 4 years ago · Juan Pablo Isaza
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