Keyboard press trigger link
- Started
- Last post
- 6 Responses
- ESKEMA
I'm having trouble implementing the keyboard arrows triggering Prev / Next links. I have tried several methods I found online but nothing is working and I'm not sure why.
link is like this:
<a id="next" href="' . $nextLinkURL . '">' . $nextLinkText . '</a>I tried to trigger it with:
$("a#next").trigger("click");
—
$("a#next").click();I tried using several jquery methods and also tried with Mousetrap.js ( http://craig.is/killing/mice )
Halp pliz
- ESKEMA0
I tried:
Mousetrap:
Mousetrap.bind('left', function(e) {
$("a#prev").click();
});
Mousetrap.bind('right', function(e) {
$("a#next").click();
});JQuery:
function leftArrowPressed() {
$("#prev").click();
}function rightArrowPressed() {
$("#next").click();
}document.onkeydown = function(evt) {
evt = evt || window.event;
switch (evt.keyCode) {
case 37:
leftArrowPressed();
break;
case 39:
rightArrowPressed();
break;
}
};
- ESKEMA0
emexbcn,
that doesn't fire the href either. If I make it do something in the js it works, but what I want is to make it fire the html "a href", which it doesn't... :/
- ESKEMA0
got it working! Thanks for the help..
$("body").keydown(function(e) {
if(e.which == 37) { // left
$("#prev").trigger("click");
}
else if(e.which == 39) { // right
$("#next").trigger("click");
}
});
$("#prev").on("click",function...
var prev = $(this).attr('href');
window.location.href = prev;
});
$("#next").on("click",function...
var next = $(this).attr('href');
window.location.href = next;
});
- ernexbcn0
Ah, the problem was we were triggering the jQuery click event not the actual browser click event.
Here's an updated version:
http://jsfiddle.net/LrsLV/1/But your solution is valid.
- ernexbcn0
http://jsfiddle.net/LrsLV/2/ this is the final one, the other edit had a function not needed