event binding on dynamically created elements javascript code example

Example 1: document on click dynamic element

$(document).on('click', '.class', function() {
    // do something
});

Example 2: javascript bind event to created element

// bind click handler to element that is added later/dynamically
document.addEventListener('click', function(e){
    if(e.target && e.target.id== 'myDynamicallyAddedElementID'){
         //do something
    }
});

//Alternatively, if your using jQuery:
$(document).on('click','#myDynamicallyAddedElementID',function(){
    //do something
});

Example 3: event after div created

$(function($) {
  $('.div_element').on('load', function() {
    $('.textbox').hide();
  }());
});