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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts

    Thumbs down Is it just me, or is javascript annoying to deal with?

    I don't understand why this trivial code won't work. I tried to write my own function to handle key events. Looking at w3schools, it seemed there was nothing to it. So I wrote my function, set up the listeners, and it didn't work. O-O

    So after some tinkering w/ my own code, I figured I HAD to be doing something wrong. So I checked someone elses code on the web, and his doesn't work either.


    html:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript" src="js/js.js"></script>
    <link rel="stylesheet" type="text/css" href="css/css.css" />
    </head>
    
    <body onkeypress="captureKey(event);">
    
    </body>
    </html>
    js:

    Code:
    function captureKey(e) {
       if (e.keyCode) {
            keycode=e.keyCode;  // works with the DOM
       }
         else {
             keycode=e.which;   // Netscape compatible
         }
       move = String.fromCharCode(keycode);
    
         alert(move);
    }
    I know that javascript is enabled because the tut that I pulled this code from works for me...it's just that the stuff I write doesnt work. I never have these weird problems except with javascript...

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    alert ("Keycode = " + key);
    
    if (key == 13) {
    alert ("You pressed the ENTER key");
    }
    
    if( ev.ctrlKey || ev.shiftKey ) {
    alert ("You pressed the Control key or the Shift Key");
    }
    
    which = String.fromCharCode(key).toUpperCase();
    if (key>=48 && key<=90) {
    alert ("You pressed the " + which + " key");  
    }
    
    }
    </script>
    I think your trouble may be using onkeypress rather than onkeydown.

    "Celtic player Garry Caldwell is enjoying a miserable few weeks at the start of the season". - Football commentator
    Last edited by Philip M; 11-07-2010 at 05:55 PM. Reason: Typo

  • Users who have thanked Philip M for this post:

    Afro_Programmer (11-07-2010)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts
    It seems like none of my code is working properly...I don't know why. Anyone have any ideas for why this is?

    -edit- It's really strange. Example, simple code like this doesn't work.

    Code:
    var y = document.getElementById("char");
    y.onclick = function() {alert("hi");}
    IE will say that y is null.
    Last edited by Afro_Programmer; 11-07-2010 at 08:01 PM.

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts
    Hi mate,

    This works fine for me on IE8 if I assign the id "char" to say a checkbox and put the code you supplied at the bottom of the document instead of in the <head>, that way it loads after the element "char" has been loaded. Without seeing the rest of your code for it, if I assume your code is in the <head> of your document then it won't work.

    Code:
    <html>
    <head>
    //head stuff incl. css
    </head>
    <body>
    <form name="form1" action="">
    
    <input type="checkbox" id="char">
    
    </form>
    <script type="text/javascript">
    
    var y = document.getElementById("char");
    y.onclick = function() {alert("hi");}
    
    </script>
    </body>
    </html>

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Yes, the element with the id must exist when the script runs.

    Either place the script after the element "char" has been loaded or wrap it in a function called onload:-
    <window onload = "functionName()">


  •  

    Posting Permissions

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