user2734550
user2734550

Reputation: 962

Keydown won't work

I have been reading many post here but I just don't get why my code doesn't work. I have another page with lightbox image slider. It has keystroke for keycode 39 and 37. Would that override my code and make it not working? I am a beginner of Jquery user, please explain it in detail.

   <span class="pageLinks">
      <asp:HyperLink ID="cmdPrev_Top" CssClass="pgprev" Text="" runat="server"></asp:HyperLink>
      <asp:HyperLink ID="cmdNext_Top" CssClass="pgnext" Text="" runat="server"></asp:HyperLink>
   </span>


   $(document).ready(function () {
    $(document).bind('keydown', function (event) {
        var keycode = event.keyCode;
        if (key == 37) {
            left(function () {
                $('.pgprev').click();
                alert(prev);
            });
        } else if (key == 39) {
            right(function () {
                $('pgnext').click();
                alert(next);
            });
        }
    }); // keydown handler ends here
});

Upvotes: 1

Views: 801

Answers (2)

Bellash
Bellash

Reputation: 8184

The variable name is keyCode not key! This code should work

$(document).ready(function () {
 $(document).bind('keydown', function (event) {
    var keycode = event.keyCode;
    if (keycode  == 37) {
        left(function () {
            $('.pgprev').click();
            alert("prev");
        });
    } else if (keycode  == 39) {
        right(function () {
            $('pgnext').click();
            alert("next");
        });
    }
}); // keydown handler ends here
});

Upvotes: 0

Jordan
Jordan

Reputation: 1433

Well, your variable names seem to be different. You have:

var keycode = event.keyCode;

Then

if (key == 37) {

And

} else if (key == 39){

Where does key come from?

Upvotes: 2

Related Questions