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.
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ?earler?? This was my first post in this thread.

    FWIW, you have two choices: Leave the JS code as it is, or indeed put in your function, thus:
    Code:
    document.onkeydown = checkKey;
    
    function checkKey( ev )
    {	
        var key;
        ev = ev || event;
        key = ev.keyCode;
        alert ("Keycode = " + key);  // for testing
    
        if(key == 38)
        {
            alert('up arrow are working');
        }
        else if(key == 40)
        {
            alert('down arrow are working');
        }
    }
    The two are 100% equivalent.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by mark103 View Post
    By the way, I want to use "<script></script>" for javascript, so would it work if i use without them??
    To use those tags you place them immediately before the </body> and then add a src="" to the script tag to identify the file that actually contains the JavaScript - as you have with the script tag immediately below the one that everyone is telling you that you don't need.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. #18
    New Coder
    Join Date
    Feb 2013
    Posts
    37
    Thanks
    1
    Thanked 4 Times in 4 Posts
    I've worked with the code and made some adjustments. It looks like it works best in the head section of the page. Below is the prototype code you can test out.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    document.onkeydown = function(ev) {	
        var key;
        ev = ev || event;
        key = ev.keyCode;
        if(key == 38){
           alert('up arrow clicked');
       }
       if(key == 40){
           alert('down arrow clicked');
       }
    }
    </script>
    </head>
    <body>
    </body>
    </html>
    I hope this helps.

  4. #19
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    How is that different from the code I posted in post #2?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  5. #20
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    Quote Originally Posted by mark103 View Post
    you should have said earlier about removing the "function checkKey(e) {" and "<script></script>" which you didn't
    PhilipM did, but you just ignored it. And even more, you stated that you implemented exactly as you were told ... which you didn't

  6. #21
    New Coder
    Join Date
    Feb 2013
    Posts
    37
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Philip M, you're right it does have the same coding -- I should of read the syntax better from the earlier post. Thanks for noting this.

  7. #22
    New Coder
    Join Date
    Jan 2013
    Posts
    71
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Make sure the script tag has been correctly placed . I think this might be the problem .


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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