0

I'm trying to simply click a link whenever someone presses the right arrow key, but nothing is happening.

I've got this link:

<a title="party pics! 16" href="/photos/photo/new-party-pics-16/" id="next-in-gallery"> <img src="http://localhost/static/photologue/photos/cache/thumbnail16.jpg"> </a> 

and this jQuery:

<script type="text/javascript"> $(document).keydown(function(e){ if (e.keyCode == 39) { $("a#next-in-gallery").click(); return false; } }); </script> 

Seems to do nothing, though. Thoughts?

2 Answers 2

2

How about

<script type="text/javascript"> $(document).keydown(function(e){ if (e.keyCode == 39) { document.location.href = $("a#next-in-gallery")[0].href; return false; } }); </script> 

?

Sign up to request clarification or add additional context in comments.

1 Comment

That'll do. Thanks! Can't neglect plain old JS.
1
$("a#next-in-gallery").trigger('click'); 

4 Comments

maybe you have to remove return false;
Nope, that doesn't seem to cut it either.
api.jquery.com/trigger look at the first example you see... that's exactly like it. Maybe there's something else changing the behavior of that anchor? Could you try it on different browsers?
I haven't tried it on any other browsers, but I'm testing in Chrome, so my confidence is pretty high with that... That's the only JS or jQuery targeting that anchor. I bet it's because the jQuery was above the link and since I didn't do it on ready, it was unable to actually grab the link before it loaded. But then again.. it's a keypress.. so I don't know.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.