Adding variables to a onclick in a for loop

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
mercwnz
Posts: 4
Joined: October 19th, 2010, 8:59 am

Example

Code: Select all

var loc = [0,1,2];
function soSomething(l){
	$.write(l);
}
for(l = 0; l < loc.length; l++){
	d.pnl.[l] = d.pnl.[c].add("panel", [0,(l*150),390,(l*150)+150]);
	d.pnl.[l].text = loc[l];				
	d.pnl.[l].btn= d.pnl.[c].[l].add("button",[155,8,300,30], "some text");
	d.pnl.[l].btn.onClick = function () {doSomething(l)} //<-- the problem..
}
I want to add a variable to the generated buttons, but if click on any of them it takes the current value of l (that will be 3).
So is there anyway around this? Because I've a whole lot of checkboxes, buttons, et cetera and I don't like to make a huge list of assigned functions to the elements.

Thanks in advance!
mercwnz
Posts: 4
Joined: October 19th, 2010, 8:59 am

Code: Select all

var dim = [50,50,500,450]
var btn = [0,50,75,50]

var d = new Window('window', 'test', [dim[0],dim[1],dim[2],dim[3]])
var y = ["morgen","overmorgen","meerdaagse","speciaal weer","renderen"];

for (var q = 0; q < y.length; q++){
	d.[q] = d.add("button",[btn[0],q*btn[1],btn[2],(q*btn[3])+btn[3]], y[q]);
	d.[q].onClick = function () {doSomething(this.text)};
}
	d.feedback = d.add("statictext",[85,5,205,60], "VARIABLES");

function doSomething(text){
	d.feedback.text = text;
}
d.show()
solved! :D and I guess I posted in the wrong category :cry:
Post Reply