Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Undefined is not an object (evaluating 'button[i].backgroundColor')

$
0
0

Hi, I didn't find out the answer to add the event "click" for the case button[i].

There is my code:

var view_body = Ti.UI.createView({
    height: "70%",
    width: Ti.UI.SIZE,
    layout: 'horizontal'
});
 
var button=[];
for (var i=0; i < 4; i++){
    button[i] = Titanium.UI.createButton({
        title: (i+1),
        height: "90",
        top: "6%",
        left: "6%",
        width: "90",
        backgroundColor: "red"
    });
    button[i].addEventListener('click',function(e){
        if(button[i].backgroundColor=="red"){
            button[i].backgroundColor="blue";
        }
        else{
            button[i].backgroundColor="red";
        }  
    });
    view_body.add(button[i]);
}
$.win.add(view_body);
Error said:
[ERROR] :      message = "undefined is not an object (evaluating 'button[i].backgroundColor')";
I have tried:
button[0].addEventListener('click',function(e){
    if(button[0].backgroundColor=="red"){
        button[0].backgroundColor="blue";
    }
    else{
        button[0].backgroundColor="red";
    }  
});
It's working, but it's only for button[0]... So if I have 100 buttons, I need too add the same funtion 100 times?...

Need your help! Thanks.


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>