Search code examples
event-handlingtitaniumappcelerator

Fire click event on button by code?


Simple enough just can't get it to work and see no info within the docs. How to fire click event on button by code?

I have tried:

btn.fireEvent('click');

The button already has an event listener, I want it to run the code within the listener when the app is in a certain state.


Solution

  • you have to add EventListner to check whether click event is fired or not,see below code ,it fires without clicking the button. you can replace commented if condition with your condition on which you want to fire btn's click event

     var win= Titanium.UI.createWindow({ backgroundColor:'white'});
            win.open();
    
        var btn= Titanium.UI.createButton({ title :' fire by code'});    
        btn.addEventListener('click',function(){
            alert('Click event fired ');
        });
    
        win.add(btn);
        //if(appState)
        btn.fireEvent('click');