Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!