Pawan Pawan - 1 month ago 5
jQuery Question

How to get the id of a dynamically created html button on its click in jquery

I have several(inside loop)

dynamically
created
html
buttons
like :

sb.Append("<input type=\"button\" name=\"deleteimage" + id
+ " \" id=\"btndelete" + id
+ "\" value=\"Delete\" class=\"t-button t-grid-delete\" "
+ " style=\"margin-left:10px;\" />");


and I want to get the
id
of the
button
on which I
click
using
jquery


Help Me

Thanks

Answer

delegate using .on()(jQuery 1.7+) or .delegate()(jQuery 1.6 and lower)

$('body').on('click','input[type=button]',function(){
     alert(this.id); // <-- this refers to current clicked input button
});

or

$('body').delegate('input[type=button]','click',function(){
     alert(this.id);
});

Or whatever your sb element is replace body with that since it exists on dom load