How to work with jQuery.Event in Firefox?

I am writing a script that fires a random keydown event with keyCode 37 when a button is clicked.

The following script works fine in IE, Safari, and Chrome, but it does not work in Firefox and Opera. Even if I changed {keyCode: 37} to {which: 37} , it still doesn't work.

$('button').click(function(e){ jQuery("body").trigger(jQuery.Event("keydown", {keyCode: 37})); e.preventDefault(); } 

Does anyone know how to make it work in Firefox and Opera?

+4
source share
2 answers

try this, if the browser does not support which , it will support keyCode

 $('button').click(function(){ var keyDownEvent = jQuery.Event("keydown"); if(keyDownEvent.which){ keyDownEvent.which = 37; }else{ keyDownEvent.keyCode = 37; } $("body").trigger(keyDownEvent); } 
+3
source

Try it like this:

 $('button').click(function(){ var e = jQuery.Event("keydown"); e.which = 50; $("body").trigger(e); }
$('button').click(function(){ var e = jQuery.Event("keydown"); e.which = 50; $("body").trigger(e); } 
+1
source

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


All Articles