Results 1 to 2 of 2

Thread: Onkeypress for cursor key

  1. #1
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Onkeypress for cursor key

    Good day!

    In my javascript code I have a function for the enter key. I want to add cursor key in that function/ I research about the coding for cursor key but I can't find the code same on what I want to happen.

    Here is the code:
    Code:
    <script language="javascript">
      function handleEnter(e, nextfield)
            {
            var characterCode = (e && e.which)? e.which: e.keyCode;
            if(characterCode == 13)
                {
                document.getElementById(nextfield).focus();
                return false;
                } 
            else
                {
                return true;
                }
            }
        </script>
    <input type='text' name='plt' onkeypress='return handleEnter(event,\"b0\");' />
    Thank you
    Last edited by rhodarose; 11-08-2010 at 08:41 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Use onkeydown. It's more reliable. The event.which isn't required by any modern browser (all Firefox, all IE, etc.).

    I'm not certain what key you mean. To determine the code you're looking for do:

    Code:
    <input type="text" onkeydown="alert(event.keyCode);">
    and hit that key while in that text input.

    Then in your function (say the code is 93 - what I suspect you're after, though even this may vary system to system):

    Code:
    <script type="text/javascript">
    function handleEnter(e, nextfield){
    	var characterCode = e.keyCode;
    	if(characterCode === 13 || characterCode === 93){
    		document.getElementById(nextfield).focus();
    		return false;
    	} else {
    		return true;
    	}
    }
    </script>
    Or, if the result from that key is desired to be different from the enter key's:

    Code:
    <script type="text/javascript">
    function handleEnter(e, nextfield){
    	var characterCode = e.keyCode;
    	if(characterCode === 13){
    		document.getElementById(nextfield).focus();
    		return false;
    	} else if(characterCode === 93) {
    		// Do some other stuff here,
    		// should include a return value of true or false to be consistent.
    	} else {
    		return true;
    	}
    }
    </script>
    Notes:

    • The code may as I say vary.
    • Taking control over any key is risky as any given system may override that and/or still also provide the default action regardless of the function you assign to it.
    • Taking control in this manner is against w3c recommendations as it may frustrate the user or worse, degrading accessibility of the page for certain handicap setups and older software/hardware.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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