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

249
Visualizações
Reset (reset to previous value) only the form fields that are changed

I need to reset only the form fields that are changed (reset to previous value).

I tried to use the reset but it completely resets the entire form and I don't need this.

How can I do this?

function clearResult() {
            document.getElementById("save").reset();
        }
<div class = "container">
         <form method="post" id="save" onload="onLoad()">
             <div class="field">
                 <label for="id"> ID:</label>
                 <input type="number" id="id" name="id" />
             </div>
             <div class="field">
                 <label for="type"> Fragment Type: </label>
                 <input type="text" id="type" name="type" />
             </div>
             <div class="button">
                 <button type="submit" class="full">Save changes</button>
                 <input type="button" value="Cancel" onclick="clearResult()" />
             </div>
         </form>
     </div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First of all you should to store the predefined values of form elements to set them back when you want to reset them back.

Then you can use this globally defined initial values to set them back whnever reset event occurs.

var id, type;
const form = document.getElementById('save');

document.onload = (event) => {
  id = form.id.value;
  type = form.type.value;
};

function clearResult() {
  form.id.value = id;
  form.type.value = type;
};
about 4 years ago · Juan Pablo Isaza Relatório

0

It was simple. When page load just get values from that fields. Make with document.onload:

var firstValue = document.getElementById("yourFieldId").value;

After form submit get values like below:

var currentValue = document.getElementById("yourFieldId").value;

And after submit in reset check if CURRENT VALUES equal with VALUES FROM FIRST TIME Example

if(firstValue != currentValue){
  document.getElementById("yourFieldId").value = firstValue;
}
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