There is some event listener in Thunderbird which resolves the emails address from my LDAP server when i press the UP or DOWN ARROW key. so i want to trigger that the same event through Javascript without having to physically press the up or down arrow key. How can i do this?
To detect the arrow key when it is pressed, use onkeydown in JavaScript. The button has key code. As you know the left arrow key has the code 37. The up arrow key has the code 38 and right has the 39 and down has 40.
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys.
To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.
I believe this is what you want (using JQuery)
var e = jQuery.Event("keyup");
// e.which is used to set the keycode
e.which = 38; // it is up
e.which = 40; // it is down
$("id_to_element").trigger(e);
If JQuery is not allowed to use, pure javascript solution is more verbose. See this answer
Note: There maybe a bug in Chrome which will hamper this. I would recommend JQuery for less headache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With