Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Left and Right Arrow keys assigned onkeydown help

    I am trying to assign the left and right arrows, but I cannot get the code to work. It would be great to get some help--I am a newbie to coding.

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    document.onkeydown=function(e) {
    var thelink
    if (e.keyCode==37) thelink='prevlink'
    else if (e.keyCode==39) thelink='nextlink'
    else return
    document.location.href=document.getElementById(thelink).href}
    </script>
    </head>
    <body>
    <a href="#" onkeydown="return previous(this)" id="prevlink">PREVIOUS</a><br />
    <br />

    <a href="#" onkeydown="return next(this)" id="nextlink">NEXT</a>

    </body>
    </html>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Whenever you say "I can not get this code to work" you should be more specific. What does not work? In which browser? What happens? What error do you get, if any?

    Anyway, this one works:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    document.onkeydown=function(e){
    var e=e||window.event; // Moz model || IE model
    var id;
    if(e.keyCode==37){id='prevlink'}
    else if(e.keyCode==39){id='nextlink'}
    else{return}
    location.href=document.getElementById(id).href;
    }
    </script>
    </head>
    <body>
    <a href="http://www.google.com" id="prevlink">Google</a>
    <br>
    <br>
    <a href="http://www.yahoo.com" id="nextlink">Yahoo</a>
    </body>
    </html>
    There are two models of capturing the events: Mozilla (inherit from old Netscape) and IE. And location is a property of the window Global Object (which usually is omitted in the code, just because it is the ultimate Object), not of the document object
    Last edited by Kor; 12-11-2010 at 10:38 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •