javascript - How to get index (or value) of DropDownButton selection -
i have downdownbutton i've populated array containing project names , ids. list shows project name, i'd project id. variable "projects" looks this:
[object { name="project a", id="1325"}, object { name="project b", id="5241"}, object { name="project c", id="3224"}]
this code creates menuitem button correctly, how set variable projid in onclick event?
for (i = 0; < projects.length; i++) { menuprojects.addchild(new menuitem({ label: projects[i].name, onclick: function () { projid = ?; } })); }
i've tried using "projid= projects[i].id;", gives me error since 3. what's correct syntax this?
-- edit --
this how got work using both cookie's , merrick's answers.
for (i = 0; < projects.length; i++) (function (x) { menuprojects.addchild(new menuitem({ label: projects[i].name, onclick: function () { projid = projects[x].id; } })); } (i));
i don't know if correct/best way it, create closure give each iteration new variable scope so:
for (i = 0; < projects.length; i++) { (function(x) { menuprojects.addchild(new menuitem({ label: projects[i].name, onclick: function () { projid = x; } })); }(i)); }
this creates anonymous function (with own scope) immediatly evaluated.
Comments
Post a Comment