IF press OR

There is a way in jQuery to have something like:

if (button.click() || (keydown == 39)) {
   //stuff
}
+3
source share
4 answers
function stuff(e) { alert('Something happened'); }

$('input[type=button]').click(function(e) { 
    stuff(e);
});

$(document).keydown(function(e) { 
    if (e.keyCode == 39) { stuff(e); }
});​​​

http://www.jsfiddle.net/4WuB5/

+8
source

You can bind () for several events:

$(button).bind("click keydown", function (evt) {
    if (evt.type == "keydown" && evt.which == 39)
        alert("Key 39 pressed");
    else if (evt.type == "click")
        alert("Clicked!");
});

Example: http://jsfiddle.net/Bymug/

Note that the space and enter keys can also trigger a click event on a button.

+7
source

live :

$('.someClass').live('keydown mouseclick', function(event) {
  if (event.type == 'mouseclick') {
      // Do something
  } else if (event.type == 'keydown' {
      if (event.keyCode == '39')
      {
          // Do something
      }   
  }
});
+3

, , . :

function x() {
  ...
}

$('.someclass')
  .click(x)
  .keydown(function(e){
    if (e.keyCode == 39) x(e);
  });
+3

Source: https://habr.com/ru/post/1764854/


All Articles