Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Multiple triggers for a single addEventListener function?

Is it possible to have multiple triggers for the same addEventListener function?

in jQuery, I can do something like this…

$(".selector").bind( "click keypress", function () {
    // do this when any of these event triggers
    // happen to the specified selector(s)...
});

It's a lot DRYer than repeating the same code over and over for each potential trigger when the outcome of the event should be the same.

Also, is it possible to add an event listener to all objects with a common selector of some sort, like the className or something?

To step it up a notch more, we could even do this and use multiple selectors with multiple event triggers… not sure how often that would happen, but both options are nice to have.

$('selectorOne','selectorTwo','etcThree').bind( "click keypress", function () {
    // do this when any of these event triggers
    // happen to the specified selector(s)...
});
— asked May 27th 2011 by Trevor Borgmeier
  • addeventllistener
  • event
  • mulitple
  • selectors
  • trigger
0 Comments

1 Answer

  • Hi Trevor,

    I'am afraid, we are not in the jQuery-world ;-)

    Untestet:

    var elems = [view1,view2,button1];
    var events = ['touch','singletap','myownevent'];
    
    for (var el=0;el<elems.length;el++) {
      for (var ev=0;ev<events.length;ev++) {
        elems[i].addEventListener(events[ev],callbackfunction);
      }
    }
    

    Hope it helps.

    Rainer

    — answered May 28th 2011 by Rainer Schleevoigt
    permalink
    3 Comments
    • Thanks. I figured that was the case, but being new to Titanium I thought it was worth inquiring as I'm finding a fair number of things it can do that aren't readily documented.

      — commented May 28th 2011 by Trevor Borgmeier
    • I am getting this error,

      Script Error = Can't find variable: callbackfunction

      Can you help clarify why this is happening?

      — commented August 29th 2012 by Paul Flood
    • you have to define what callbackfunction is

      — commented September 19th 2012 by Ronnie Swietek
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.