Javascript onclick trigger key click (using jQuery)

How can I activate the keyboard arrow "left" or "right" by clicking on the div button.

So for example

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

(I know 37 left)

+4
source share
3 answers

Would you like to

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

You can, of course, replace document.body with any other node that is associated with a keypress or keydown .

Link: .trigger()

+7
source

From The final way to trigger keypress events using jQuery :

 var e = jQuery.Event("keypress"); e.which = 37; // # Some key code value $("div").trigger(e); 
+2
source

not sure, but you can try this.

 $('.item').click(function(){ $('body').trigger("keypress", [{ preventDefault:function(){}, keyCode:13 }]); }); 
+1
source

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


All Articles