the first one works because you use direct on event and do not delegate it to any of your parent elements
$('input#doIt').on('click', { name: "Karl" } , function(event){
here you simply fire the click event for input with the id doIt , which will not work if the input is added dynamically ..
and in the second - delegation, but then to the same element.
$('input').on('click', '#doIt' , { name: "Karl" } , function(event){ alert(event.data.name); });
here, this will trigger a click event for all elements with doIt id inside the input, which is incorrect
you need to delegate it to the closest static parent container .. so this will work.
$('div#goTo').on('click', 'input#doIt' , { name: "Karl" } , function(event){ ...
note: you have two elements with the same Id doIt , which is invalid HTML .. change it to a class .. I changed the class entry here in the code above. strike>
source share