0

I am having problems with a jquery slideshow where I want to bind next/right to make the images jump forwards/back.

I used the following code to attempt to do it but it simply refreshes the page.

<script>
$(function() {$(document).keyup(function(e) {
switch(e.keyCode) { case 37 : window.location = $('#prev a').attr('href'); break;
case 39 : window.location = $('#next a').attr('href'); break; }});});
</script>   

The I was attempting to call were:

<a href='#' id='prev' onclick='show_image($prev); return false;'>

and

<a href='#' id='next' onclick='show_image($next); return false;'>

Does anyone know an easy way to cut out the middle man and simply bind left/right to the onclick event for each?

Anyhelp would be much appreciated!

tjh
  • 89
  • 1
  • 6

2 Answers2

0

This should work:

$(function() {
    $(document).keyup(function(e) {
        switch (e.keyCode) {
        case 37:
            $('#prev a')[0].onclick();
            break;
        case 39:
            $('#next a')[0].onclick();
            break;
        }
    });
});​

Just call the onclick event handler of the left or right button based on which key you pressed.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
0
$(function() {
    $(document).keyup(function(e) {
        switch (e.keyCode) {
        case 37:
            $('#prev').click();
            break;
        case 39:
            $('#next').click();
            break;
        }
    });
});​
Frenchi In LA
  • 3,139
  • 3
  • 25
  • 41