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.
2 Answers
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
Comments
$('a').trigger('click'); 3 Comments
Derek Lerner
I tried... $(document).keydown(function(e){ if (e.keyCode == 37) { $("#next").trigger('click'); } }); To trigger a link formatted similar to below... <a accesskey='n' href='URL'><img src='URL.gif' alt='Next >' title='Next >' id='next' /></a> ...and not having any luck. Any ideas?
David Hellsing
@Derek: try putting the id #next in the A tag instead of the IMG tag.
Derek Lerner
@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.