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 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onkeypress event to submit a form?

    Edit: ** SOLVED **



    I have this form:
    Code:
    <form action="index.php?mid=72me211v4604" method="POST">
    <textarea name="msg"></textarea>
    <br>
    <input type="submit" value="S&#228;nd/uppdatera">
    </form>
    Useing <textarea></textarea>, is there a way to make it submite when you hit <Enter>?

    With <textarea></textarea>, when you hit <Enter> it jumps down a line. I don't want it to do that. I want the form to submit when the enter key is pressed.

    I'm thinking "onkeypress" event. How do I do this? I know nothing about this event.

    Thanks!
    Last edited by Bob Leny; 08-02-2006 at 04:45 AM.

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    Code:
    <script language="JavaScript">
    function submitonEnter(){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    if(keycode == "13"){
    document.form1.submit();
    }
    }
    </script>
    <form name="form1" action="index.php?mid=72me211v4604" method="POST">
    <textarea name="msg" onKeyDown="submitonEnter();"></textarea>
    <br>
    <input type="submit" value="S&#228;nd/uppdatera">
    </form>

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by boeing747fp
    Code:
    <script language="JavaScript">
    function submitonEnter(){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    if(keycode == "13"){
    document.form1.submit();
    }
    }
    </script>
    <form name="form1" action="index.php?mid=72me211v4604" method="POST">
    <textarea name="msg" onKeyDown="submitonEnter();"></textarea>
    <br>
    <input type="submit" value="Sänd/uppdatera">
    </form>
    Thanks! But It seems to only work in IE.

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try changing the first part of this function to:
    Code:
    function submitonEnter(e){
    var keycode = (!e)?window.event.keyCode:e.which;
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pyth007
    Try changing the first part of this function to:
    Code:
    function submitonEnter(e){
    var keycode = (!e)?window.event.keyCode:e.which;
    Nope didnt work...

    This script works on FF IE and Opera...
    PHP Code:
    <html>
    <
    body>
    <
    script type="text/javascript">
    function 
    noNumbers(e)
    {
    var 
    keynum
    var keychar
    var numcheck

    if(window.event// IE
    {
    keynum e.keyCode
    }
    else if(
    e.which// Netscape/Firefox/Opera
    {
    keynum e.which
    }
    keychar String.fromCharCode(keynum)
    numcheck = /d/
    return !
    numcheck.test(keychar)
    }
    </script>

    <form>
    <input type="text" onkeydown="return noNumbers(event)" />
    </form>

    </html> 
    But, I don't know how to addapt it. I just don't know any of that. I only know really basic stuff.

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    this script here works for me in IE and FF.

    Code:
    <script language="JavaScript">
    function submitonEnter(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(charCode == "13"){
    document.form1.submit();
    }
    }
    </script>
    <form name="form1" action="index.php?mid=72me211v4604" method="POST">
    <textarea onKeyPress="javascript:return false;" name="msg" onKeyDown="javascript:return submitonEnter();"></textarea>
    <br>
    <input type="submit" value="S&#228;nd/uppdatera">
    </form>
    Last edited by boeing747fp; 08-02-2006 at 03:58 AM.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by boeing747fp
    this script here works for me in IE and FF.

    Code:
    <script language="JavaScript">
    function submitonEnter(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(charCode == "13"){
    document.form1.submit();
    }
    }
    </script>
    <form name="form1" action="index.php?mid=72me211v4604" method="POST">
    <textarea onKeyPress="javascript:return false;" name="msg" onKeyDown="javascript:return submitonEnter();"></textarea>
    <br>
    <input type="submit" value="Sänd/uppdatera">
    </form>
    Are you sure it works in Firefox? You should have passed the event object in the function for it to work.
    Code:
    onKeyDown="javascript:return submitonEnter(event);"
    Ben, why do you want to submit the form on press of Enter in the textarea? How can the user add new lines in the textarea then? If you dont want new lines, just use text field. Using text field, you dont need the code for auto submission as by default, forms with single text field will automatically be submitted on press of Enter in the field.

  • #8
    New Coder
    Join Date
    Jul 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright! Thanks this works!

    PHP Code:
    <html>
    <
    body>

    <
    script language="JavaScript">
    function 
    submitonEnter(evt){
    var 
    charCode = (evt.which) ? evt.which event.keyCode
    if(charCode == "13"){
    document.form1.submit();
    }
    }
    </script>

    <form name="form1" action="index.php?mid=72me211v4604" method="POST">
    <textarea name="msg" onKeyDown="javascript:return submitonEnter(event);"></textarea>
    <br>
    <input type="submit" value="Sänd/uppdatera">
    </form>

    </body>
    </html> 

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    o crap i forgot that "event" :P but yea it was in there when i first pasted it :P


  •  

    Posting Permissions

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