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

81
Vistas
Event handling in javasript

I've made this little index.html to ask how are event handling working on javascript.
As you can see at the end I declared an event and than add dynamicly the element to my DOM.
The problem with this is that the event handler seems to not taking in account once you add the element that match with an event.
I tried to use .on() beside of .click() but no change in the result. S The question is how to processed to add event to an element that is dynamicly add to the DOM.
Is there a solution to reload or add the event to the event handler.
Or should I just always manage to declare my event only after i declare my element and disable it once I remove the element.

TLDR :
I try to find a way to declare an event and than add the matched DOM element is it possible ?

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.13.0-rc.3/jquery-ui.js" integrity="sha256-tYLuvehjddL4JcVWw1wRMB0oPSz7fKEpdZrIWf3rWNA=" crossorigin="anonymous"></script>
  </head>
  <body>
  </body>
</html>
<script type="text/javascript">


  $("#searchBar").autocomplete({
    source: [ "Pomme", "Poire", "Fraise", "Banane" ],
  });

  $( "#searchBar" ).on( "autocompleteselect", function( event, ui ) {
    console.log("ok");
  });

  $("#btn").on("click", function() {
    console.log("click");
  });

  $("body").append('<input id="searchBar" type="text" name="" value="">');
  $("body").append('<button id="btn" type="button" name="button">Bouton</button>');


</script>
about 4 years ago · Juan Pablo Isaza
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