Can some one help me? The Idea is to create dynamic buttons with a loop and then use the jquery click function to use one of them
//I'm creating dynamic buttons like this:
for(i=0; i<1000; i++){
$contentBox.append('<button id="add'+ i +'" type="button" class="btn btn-success">Accept</button>');
//but how would I create the jquery click function?
$('#add'+i).click(function(e) {....});
//this does not create 1000 click functions. It only changes the id to the last one so what ever button you click on you will always get the las id
}
$(document).on('click', '#add'+i', function(e){ ... })for dynamic elements. You can refer to the SO docs about delegated events. I had an example going over adding events handlers to dynamic elements specifically, although it was never accepted/removed. Anyways just change$('ul')to$(document)in the docs example for it to work in any case.$(document).on(click: function(){}, '#add' + i)