Sunday, 15 September 2013

How do you assign n buttons to jQuery dialog in a loop?

How do you assign n buttons to jQuery dialog in a loop?

The code below does something odd. Whenever you click a button in the
dialog, you see "test5" as the alert text, not "test0", "test2", ...
"test4" respectively for each button. Something about assigning the
function in a loop is not working.
var arrbuttons = [];
for (var i=0; i<5; i++) {
arrbuttons.push({click: function() { alert('test'+i);}, text:'test'+i});
}
jQuery("#requestdialog").dialog(
{
title: "test",
height: 250,
width: 500,
modal: true,
buttons: arrbuttons
}

No comments:

Post a Comment