Can anyone tell me why this function is not working ?
for(var i=1;i<=12;i++)
{
var btn=$('<div>Button</div>').attr('id', 'id_'+i).button();
btn.css('margin','10px').on('click', showId());
btn.appendTo($('#buttons'));
}
function showId(){
alert($(this).attr('id'))
}
You want to pass the function, not run it:
BTW, you should also cache your
$('#buttons'), and use event delegation:A few more things to consider: