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

210
Visualizações
Clear specific input fields with jQuery

I am trying to clear certain fields in my form.

I have used the following jQuery code, but they do not work for me:

My HTML

<section id="clear-section" class="clear-section">
    <a id="clear-link" href="javascript:void(0)">Clear</a>
</section>

jQuery Code 1

let clearLink = $("#clear-link");
clearLink.on("click", function () {
  $("#calc-form-section-1")
    .find('input[type="text"],input[type="number"]')
    .each(function () {
      $(this).val("");
    });
  });

jQuery Code 2

clearLink.on("click", function () {
    let fieldsArray = [
      totalHomeSqftInput,
      calcRoofSqftInput,
      annualKwInput,
      calcKwInput,
      systemSizeInput,
      roofCompInput,
      pwrWallBattInput,
      totalCostInput,
      roofPriceBeforeItc,
      estTotalBeforeItc,
      estItc,
      pwrWallPriceBeforeItc,
    ];
    
    $.forEach(fieldsArray, function () {
      $(this).trigger("reset");
    });
});

Any help would be greatly appreciated.

Thank you

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

0

One approach would be to label the input elements that you wish to clear with a data-clear attribute. You can use a selector to search for these input values and clear them using an each call.

See demo:

$("#clear-link").on("click", function(e) {
  $("#calc-form-section-1 input[data-clear='true']").each(function(i) {
    $(this).val("");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="clear-section" class="clear-section">
  <form id="calc-form-section-1">

    <input type="text" data-clear="true" id="totalHomeSqftInput" value="abc" /><br />
    <input type="text" data-clear="true" id="calcRoofSqftInput" value="abc" /><br />
    <input type="text" data-clear="true" id="annualKwInput" value="abc" /><br />
    <input type="text" data-clear="true" id="calcKwInput" value="abc" /><br />
    <input type="text" data-clear="true" id="systemSizeInput" value="abc" /><br />

    <input type="text" id="other" value="abc" /><br />

    <a id="clear-link" href="javascript:void(0)">Clear</a>
  </form>
</section>

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