2

In Jquery I would like to know how to trigger/click an a href... link that's within a .php page. This page is dynamically generating two links that I'd like to trigger using left and right keyboard arrow keys for paging next and back through a site. The links are two images within two divs. I attempted to use this but was unable to get to work due to how the links are being created.

Derek Lerner
  • 23
  • 1
  • 3

2 Answers2

8

I could do something like this:

$(document).keydown(function(e){
    if (e.keyCode == 37) { 
       alert( "left pressed" );
       return false;
    }
});

Character codes:

37 - left

38 - up

39 - right

40 - down

Community
  • 1
  • 1
jbochi
  • 28,816
  • 16
  • 73
  • 90
3
$('a').trigger('click');
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • I tried... $(document).keydown(function(e){ if (e.keyCode == 37) { $("#next").trigger('click'); } }); To trigger a link formatted similar to below... Next > ...and not having any luck. Any ideas? – Derek Lerner Dec 31 '09 at 23:00
  • @Derek: try putting the id #next in the A tag instead of the IMG tag. – David Hellsing Jan 01 '10 at 10:49
  • @David Thanks for the help. Unfortunately I'm still unable to trigger a link via arrow keys after changing the location of the ID to the A tag. The code @jbochi shared works for displaying an alert, I'm just not able to figure out how to get this script to select and trigger a link. – Derek Lerner Jan 01 '10 at 18:45