www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 01-02-2005, 01:10 PM
    zaajats zaajats is offline
    geek
     
    Join Date: Jan 2005
    Posts: 4
    Form submit on enter, allow newlines

    Hi,

    I have a form with one <textarea> field that submits the form when the user presses "enter". But I would like that users could enter newlines by pressing ctrl+enter, shift+enter or some similar combination.

    Is that possible, and if it is, then how can I do it?

    The current code is the following:

    ...
    <script type="text/javascript">
    <!--
    function submitenter(myfield,e)
    {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13)
    {
    document.chat_form.submit();
    return false;
    }
    else
    return true;
    }
    //-->
    </script>
    ...
    <textarea onKeyPress="return submitenter(this,event)"> </textarea>
    ...
    Reply With Quote
      #2  
    Old 01-02-2005, 01:37 PM
    pj59 pj59 is offline
    Little Brother
     
    Join Date: Dec 2004
    Posts: 364
    Hello!

    From my point of view, your approach is not logical. You avoid the normal "newline" in a form field and have the form submitted on pressing enter. Wouldn't it be much more practical to leave the textarea's functionality untouched and provide an "as-easy-as-possible" way for the user to submit the form without using his mouse?

    You would have to tell the user how to do that, just like you would if the new line required something "special".

    PJ
    __________________
    &nbsp;
    Reply With Quote
      #3  
    Old 01-02-2005, 02:23 PM
    Charles's Avatar
    Charles Charles is offline
    JavaScript Banned
     
    Join Date: Nov 2002
    Location: Baltimore, Maryland
    Posts: 11,667
    It's a standard practice in programming to use "\n" for a new line. It might work with real people too.
    __________________
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web
    Reply With Quote
      #4  
    Old 01-02-2005, 04:47 PM
    zaajats zaajats is offline
    geek
     
    Join Date: Jan 2005
    Posts: 4
    The code is for an instant messenger type application, where it would be convenient for the user to use [enter] to send the message.
    But in, for example MSN messenger, I often use line breaks (ctrl+enter) to make my text clearer, so I thought this would be a useful function.
    Reply With Quote
      #5  
    Old 01-03-2005, 06:03 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,143
    try:

    using also onkeydown/onkeyup events to modify a parameter in case SHIFT or CTRL is pressed/released. Use this parameter as a condition to not submit/submit form when Enter is pressed.

    something like:

    var param;
    function checkE(e){
    if(SHIFT or CTRL is pressed)
    param = 1;
    if(SHIFT or CTRL is released)
    param = 0;
    }

    function submitenter(myfield,e)
    if(param==0){
    ... now the rest of your code...

    ...

    <textarea onKeyPress="return submitenter(this,event)" onkeydown="checkE(event)" onkeyup="checkE(event)"> </textarea>
    Reply With Quote
      #6  
    Old 01-03-2005, 06:45 AM
    pj59 pj59 is offline
    Little Brother
     
    Join Date: Dec 2004
    Posts: 364
    I would not use ctrl+enter for the line breaks. Both Mozilla and IE wouldn't deal with it. Opera does so (depending on version and settings).

    Shift+enter would be better due to that. If you like, I can make a little script which does what you want.

    PJ
    __________________
    &nbsp;
    Reply With Quote
      #7  
    Old 01-03-2005, 07:45 AM
    zaajats zaajats is offline
    geek
     
    Join Date: Jan 2005
    Posts: 4
    Quote:
    Originally posted by pj59
    I would not use ctrl+enter for the line breaks. Both Mozilla and IE wouldn't deal with it. Opera does so (depending on version and settings).
    Shift+enter would be better due to that. If you like, I can make a little script which does what you want.
    pj59,
    I would really appreciate if you wrote the script, I am not really an expert in javascript.

    Thanks in advance.
    Reply With Quote
      #8  
    Old 01-03-2005, 08:11 AM
    pj59 pj59 is offline
    Little Brother
     
    Join Date: Dec 2004
    Posts: 364
    Hello!

    Just the basics:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <
    html>
    <
    head>
    <
    title>Untitled</title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    var
    isShift=null;
    var
    isNN = (navigator.appName.indexOf("Netscape")!=-1);
    var
    OP = (navigator.appName.indexOf("Opera")!=-1);
    if(
    OP)isNN=true;
    var
    key;
    function
    shift(event){
    key = (isNN) ? event.which : event.keyCode;
    if (
    key==16)isShift=1;
    }
    function
    process(event){
    key = (isNN) ? event.which : event.keyCode;
    if(
    document.layers&&event.modifiers==4){
    isShift=1;
    }
    if (
    key==13&&isShift!=1){
    document.myForm.submit();
    }
    if (
    key!=16)isShift=null;
    }
    function
    retrieve(){
    document.myForm.text1.focus();
    if(
    location.search.length>0){
    blubb=unescape(location.search.split('=')[1]);
    document.myForm2.text2.value=blubb;
    }
    }
    //-->
    </script>
    </head>
    <body onload="retrieve()">
    <form name="myForm">
    <textarea name="text1" onkeypress="process(event)" onkeydown="shift(event)" cols="50" rows="7"></textarea><input type="submit" accesskey="s" value="Send">
    </form><br><br>
    This second textarea is just to show, that the line breaks "arrive":<br>
    <form name="myForm2">
    <textarea name="text2" cols="50" rows="7"></textarea>
    </form>
    </body>
    </html>
    Tested in Opera (5.02 and 6.05), Netscape (4.7, 6.2.2 and 7.02), Mozilla 1.5 and IE 6.

    Apart from the focussing of the upper textarea, the function retrieve and its' call in the body tag are just for playing, just like the second form with the lower text area.

    Regards PJ
    __________________
    &nbsp;
    Reply With Quote
      #9  
    Old 01-03-2005, 08:39 AM
    zaajats zaajats is offline
    geek
     
    Join Date: Jan 2005
    Posts: 4
    complete

    Works like a charm, thanks a lot PJ
    Reply With Quote
      #10  
    Old 01-03-2005, 08:40 AM
    pj59 pj59 is offline
    Little Brother
     
    Join Date: Dec 2004
    Posts: 364
    You're welcome!

    PJ
    __________________
    &nbsp;
    Reply With Quote
      #11  
    Old 02-21-2009, 02:42 AM
    Coruba67 Coruba67 is offline
    Registered User
     
    Join Date: Feb 2009
    Posts: 6
    Hi guys, I know this thread has been dead for a while now, its just I went through it with the same issue, however my browser is reporting that

    "The object doesn't support this property or method"

    referring to the part in the javascript

    document.sendmessage.submit();

    sendmessage being the name of my form. Any idea's what gives?
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:04 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.