javascript - Go to link when pressing keyboard arrows -
i have 2 links: <a href='leftlink.php'><< prev</a>
, <a href='rightlink.php'>next >></a>
.
can possible go leftlink when pressing left arrow keyboard
, , rightlink.php when pressing right arrow keyboard
? advice welcome
thank in advance.
give both links id , setup listener keyup event, check keycode pressed appropriate link, , trigger click on it.
html
<a id="prevlink" href='leftlink.php'><< prev</a> <a id="nextlink" href='rightlink.php'>next >></a>.
js
document.addeventlistener("keyup",function(e){ var key = e.which||e.keycode; switch(key){ //left arrow case 37: document.getelementbyid("prevlink").click(); break; //right arrow case 39: document.getelementbyid("nextlink").click(); break; } });
Comments
Post a Comment