4

How can I activate a "left" or "right" keyboard arrow push on click of a div.

So for example

$('.item').click(function(){
    keyCode(37);
});

(I know that 37 is left)

rickyduck
  • 4,030
  • 14
  • 58
  • 93

3 Answers3

7

You would go like

$('.item').click(function(){
    $( document.body ).trigger({
        type: 'keypress',
        which: 37,
        keyCode: 37
    });
});

You can of course replace document.body with any other node that has a keypress or keydown event bound to it.

Reference: .trigger()

jAndy
  • 231,737
  • 57
  • 305
  • 359
2

From Definitive way to trigger keypress events with jQuery:

var e = jQuery.Event("keypress");
e.which = 37; // # Some key code value
$("div").trigger(e);
Community
  • 1
  • 1
paulslater19
  • 5,869
  • 1
  • 28
  • 25
1

not sure, but can you try this.

$('.item').click(function(){ 
      $('body').trigger("keypress", [{ 
        preventDefault:function(){}, 
        keyCode:13 
     }]); 
});
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50