Codler
Codler

Reputation: 11256

How to trigger only if it match 2 events in Jquery?

I want to have a event that only triggers when you press space and move your mouse at the same time. How can you make that?

Upvotes: 1

Views: 97

Answers (2)

googletorp
googletorp

Reputation: 33265

You could make one event that sets a variable to true when space is pressed down, and to false when it is released. Then you can check that variable in your mouseclick event.

Using closure, you can put this in your document ready function. This will work when clicking on the DOM with id "container"

(function () {
   var space = false;
   $("document").keyup(function(e) {
     if (e.keyCode == 32) {
       space = false;
     }
   }).keydown(function(e) {
     if (e.keyCode == 32) {
       space = true;
     }
   });
  $("#container").click(function() {
    if (space) {
      // Do action
    }
  });
})();

Upvotes: 1

Related Questions