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

78
Visualizações
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 à 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