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

157
Vistas
When I'm clicking on input filed it works, but if I add new fields of input field the blur function on that is not working How to solve this by jquery

html code

<div id="add-code">
<input type="text" class="code">
</div>
<a id="new-code"></a>

java script code

<script>
$(document).ready(function(){$("#new-code").click(function(){$("#add-code").append(`<input type="text" class="code">`)
});
$(".code").blur(function(){$(this).remove()})
})
</script>

here I have made a on click function for my tag for adding more input fields and blur function on code class for removing that input field but as I add more fields, the new added input fields do not support blur function

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You were actually very close. You were forgetting to add the event listener to the new elements when you were creating them.

I have corrected your function here

$("#new-code")
.click(function(){
    $("#add-code").append(
        $(`<input type="text" class="code">`).blur(function(){$(this).remove();})
    );
});

$(".code")
.blur(function(){
    $(this).remove();
});

Because these elements were created after your JavaScript loaded they never get the event listener. Therefore when we create the new elements we must bind the event listener to them before appending to the container.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Because blur event listener is only called once, so it's active only on the currently existing inputs.
You need to call it each time you append a new input like this:

$(document).ready(function () {
  $("#new-code").click(function () {
    $("#add-code").append(`<input type="text" class="code">`);
    
    $(".code").last().blur(function () {
      $(this).remove();
    });
  });
});

Working example => https://codepen.io/moaaz_bs/pen/NWwGzvy?editors=1010

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