jquery - id of dynamically created button undefined -
i have dynamically created buttons:
<input type='button' id='remove_"+ii+"' class='removeitem' value='remove'>
the value of ii irrelevant.
i trying listen of these buttons clicked , something:
$(document).on('click',$('.removeitem'),function(){ alert($(this).attr('id')); });
however, alert giving me "undefined". @ least expect see "remove_"..
what missing?
i think problem way registering event handler. in case passing jquery object second parameter while trying use event delegation.
for event delegation need pass string selector second param .on()
$(document).on('click', '.removeitem',function(){ alert($(this).attr('id')); });
demo: fiddle
why?
because on() method allows pass object parameter, passed handler in event.data
property, handler instead of using event delegation adding handler document
object event data jquery object. this
inside handler referring document
object not have id
property
demo: fiddle