T
T
thehighhomie2015-11-09 19:35:25
JavaScript
thehighhomie, 2015-11-09 19:35:25

Button event on elements?

How can I make it so that when an element, such as a DIV, is clicked, the keyboard up and down arrow press actions are triggered? For example, so that you can switch slides by clicking on the arrows (DIVs) and the actions of pressing the arrow buttons on the keyboard work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nepritimov, 2015-11-09
@thehighhomie

With jQuery:

var keyUp  = $.Event("keydown", { keyCode: 38 }),
    keyDown  = $.Event("keydown", { keyCode: 40 });
$(".div-up").trigger(keyUp);
$(".div-down").trigger(keyDown);

Try it, maybe this will work for js:
var evt = document.createEvent("Event");
evt.initEvent("keyup", true, true);
evt.keyCode = 38;
function a(e) {
    alert(e.keyCode);
}
document.onkeyup = a;//функция при нажатии клавиши
document.dispatchEvent(evt);//срабатывание

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question