Generating jQuery dialog buttons from an array of values -


i'm trying dynamically create jquery ui dialog buttons derived array passed function, , pass key of selected button next function on button click, having no luck far.

a simple test case:

var buttons = {}; (var o in options) {   buttons[options[o]]=function(){     alert(o);   } } 

the labels of button correct, on click alert() displays key of last element in buttons array.

i've tried different tricks (copying o local variable, etc.), no avail.

any ideas on how achieve this?

your variable o bound scope of foo loop, meaning when alert gets called retains last value assigned it.

as you're using jquery, can use $.each solve this:

var buttons = {}; $.each(options, function(i, v) {      buttons[v] = function() {          alert(v);      } }); 

the v equivalent options[o] own code, without being tied scope of loop variable.


Comments