I'm relatively new to javascript and I've been banging my head against an issue all day - I'm hoping someone can point me in the right direction.
I have a PHP script that does a slideshow. I have NEXT and PREVIOUS buttons which contain dynamic links to another javascript function. I'm hoping to find a way of triggering them with the arrow keys on the keyboard.
So far, I've found a way of making the arrow keys launch particular URLs but I'm struggling a bit now. The target URLs change as a result of hiding and showing DIVs on the page. So I think I need to do one of the following:
Trigger this code with URLS constructed within PHP variables later in the main body of the page - without refreshing the page:
<script type="text/javascript">
$(document).keydown(function(e){
//e.which is set by jQuery for those browsers that do not normally support e.keyCode.
var keyCode = e.keyCode || e.which;
if (keyCode == 37)
{
window.location = 'http://www.domain.com/previous-image.jpg';
return false;
}
if (keyCode == 39)
{
window.location = 'http://www.domain.com/next-image.jpg';
return false;
}
});
</script>
...or I need to find a way of triggering a link within a DIV of a particular name - but only if the DIV is not hidden.
Any pointers would be appreciated.
Edit: 10/10/12
Thanks for everyone's help so far. I now understand a bit more and I can perhaps be clearer with what I'm trying to achieve.
I have a page which has 5 DIVs - 4 of which are hidden. Each is identical except for the image displayed in it and I use the following code to hide or show the various DIVs: http://jsfiddle.net/edzpgt/ZhjRc/12/
I need to use arrow keys to navigate the pages so I'll add some Jquery for this as we've already discussed: http://jsfiddle.net/edzpgt/b2bWz/3/
I think I have two issues: 1. I'm not sure I have the syntax correct in order to make sure I'm only using the arrow key links from the visible DIV. 2. I don't think I'm setting or using the next/previous page number variable correctly in order to get the right desintation URL passed to the Jquery.
Can anyone help please?