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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Onkey enter keypress

    I want to have an event handler where if the "enter" key is pressed while in a specified text field . run a function for example.
    say if i have
    Code:
    <input type="text" id="textfield">
    They type in the field whatever they want. and as soon as the "Enter" button is pressed (i belive enter is 13 or 10) , alert the value of the text field
    Last edited by WebmasterLULZ; 01-17-2009 at 03:33 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Here you are:-
    Code:
    <input type = "text" id = "textfield" size = "20" onkeyup = "chk(event)">
    
    <script type = "text/javascript">
    
    function chk(ev) {
    var key;
    ev = ev || event;
    key = ev.keyCode;
    if (key == 13) {
    var val = document.getElementById("textfield").value;
    alert ("You pressed the Enter key\nYou have entered " + val);
    }
    }
    
    </script>

    "A psychiatrist asks a lot of expensive questions that your wife asks for nothing." – Joey Adams
    Last edited by Philip M; 01-17-2009 at 07:54 AM. Reason: Rangana's correction added

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You beat me Philip, but one thing:
    Code:
    <input type = "text" id = "textfield" size = "20" onkeyup = "chk(event)">
    ...will make it work on FF too.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    Philip M (01-17-2009)


  •  

    Posting Permissions

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