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 2005
    Location
    detroit area
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    pressing enter in <textarea> box

    hi all, i have a form that has a <textarea> box. when a user presses enter i want it to submit the form instead of breaking to the next line and inserting a cariage return in the form data. i do not have a submit button and instead use a image with javascript link, do i have to use a submit button for this to work? this is probably better put in the javascript section. thanks.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">           
    function ifEnter(field,event) {
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (theCode == 13){
    document.forms[0].submit();
    return false;
    } 
    else
    return true;
    }      
    </script>
    </head>
    
    <body>
    <form>
    <textarea cols="10" rows="5" onkeypress="ifEnter(this,event);"></textarea>
    <input type="image" src="someimage.jpg">
    </form>
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a JS question (well, and so was the original) but...

    Aero, could you explain this line a little more:

    Code:
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    I am familiar with this syntax:

    Code:
    var someVar = (...some condition to check...) ? valueIfTrue : valueIfFalse;
    Your syntax has some extra details I'm not familiar with.

  • #4
    New Coder
    Join Date
    Jul 2005
    Location
    detroit area
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">           
    function ifEnter(field,event) {
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (theCode == 13){
    document.forms[0].submit();
    return false;
    } 
    else
    return true;
    }      
    </script>
    </head>
    
    <body>
    <form>
    <textarea cols="10" rows="5" onkeypress="ifEnter(this,event);"></textarea>
    <input type="image" src="someimage.jpg">
    </form>
    </body>
    </html>
    awesome thats what i need. now what about the shift-enter thing? what would the charcode be for something like that? and what about users with js disabled(not worried about it just curious)?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    I am familiar with this syntax:

    Code:
    var someVar = (...some condition to check...) ? valueIfTrue : valueIfFalse;
    Your syntax has some extra details I'm not familiar with.
    That's a compound statement - it uses two, nested, ternary operators. It's clearer when you add parentheses:

    Code:
    var theCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
    So if the first test (event.keyCode) fails, the second value is used, which is itself the result of another ternary operator. If the second test (event.which) fails, the final value (event.charCode) is used.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Grant said it quite well.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_
    Grant said it quite well.
    Thank you, thank you. I'll be here all week.

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, thank you Grant. I understood that concept before your explanation, but it was the lack of parenthesis that threw me in the original snippet.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ]|V|[agnus
    Yes, thank you Grant. I understood that concept before your explanation, but it was the lack of parenthesis that threw me in the original snippet.
    Those nested ternary statements can get really tricky to understand properly, and adding parentheses always help the reader!


  •  

    Posting Permissions

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