jQuery on dynamic element

Problem with jQuery click function:

$(".selector").click(function(){
  alert("yami code");
})
  • Poblem: The problem with the jquery click function is that it's attached to the element on the first run of the javascript. So, any dynamic element created later will not have the on click listener
  • Solution: The good solution is to set the click to a non dynamic element using the on function provided by jquery

Solution to jQuery click function:

$("body").on("click", ".selector", function(){
  alert("yami code");
})