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

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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