Listen to multiple events in jQuery

Asked

Viewed 470 times

2

Is there any simplified way to make a selector listen to various types of events?

Example:

$('meu-seletor').on(['click','touchstart','keyup'],function(){
    // minha ação aqui
});

Currently the only solution I know would be to create multiple .on() each calling a single event. It is possible to do it in some other way?

1 answer

3


Yeah, just separate with spaces:

$('meu-seletor').on('click touchstart keyup', function(){
    // minha ação aqui
});

In the jQuery’s documentary says:

One or more space-separated Event types
One or more event types separated by spaces

  • 1

    Put, really this?! Much simpler than I imagined.

  • 2

    @Kazzkiq But beware that it has device that fires both the click and the touchstart. Then your System will rotate bent.

  • Well viewed @bfavaretto!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.