I generate the set of buttons within html table as follows and then I want to call to function when it click.
$.each(childData, function(key, item) {
var packPath = key.replace(/_/g, "/"); //Replace underscore with slash
div.innerHTML = div.innerHTML + '<td>'+key+'</td>'
+ '<td><button type="button" data-id="'+key+'" class="download btn btn-success btn-xs">Originals</li></td></div>';
})
This is how I call the function but it's not working.
$(".download").click(function(){
alert();
});
Where is the wrong in above code?
Try this:
You need to use event delegation.
If your table has an id of "button-table", you can create an event handler like so:
Do you want it this way? I have given code for adding an entire table.Check this out
Delegate the event to static parent:
Here
div
can be the static parent which was available when page was loaded at first load. Althoughdocument
orbody
can also be used in place ofdiv
.As you have not presented how you create
div
element but one thing has to be noticed that you are generating an invalid markup. As atd
element can't be a child ofdiv
buttable
'str
.