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


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -