Using Titanium Appcelerator I am trying to dynamically create elements and add event listeners to them using a loop. Here is my current code:
for(i=0;i<7;i++){
testLabels[i] = Titanium.UI.createLabel({
borderRadius: 35,
text:'hello',
textAlign:'center',
width:70,
height: 70,
top: '13%',
left:140,
touchEnabled: true
});
testLabels[i].addEventListener('click',function(e){
//do something
}
}
When I run this I get the following error:
Can't find variable: testLabels.
Its interesting to me that the variable it can’t find isn’t “testLabels1”, which to me means the loop isn’t firing… any ideas?
Thanks!
Titanium doesn’t like it when I place “var” in front of the label declaration.
try this