retrieving current input field

Discussion in 'Javascript' started by please-answer-here, May 26, 2005.

  1. Scenario:

    I'm using the following code to avoid that users use the return key in the
    middle of a form to submit the entire form
    ---- snip ---------
    function kH(e) {
    var pK = document.all? window.event.keyCode:e.which;
    return pK != 13;
    }

    document.onkeypress = kH;
    if (document.layers) document.captureEvents(Event.KEYPRESS);
    ---- snip ---------

    What I would do is to modify the code so that when the users is in a
    textarea input field they are allowed to use the return key. Is there a
    funcion/method I can use in the function to test in which named input field
    is the active one?
     
    please-answer-here, May 26, 2005
    #1
    1. Advertising

  2. please-answer-here

    askMe Guest

    Curious. Why aren't you using an onsubmit event? Or are you? I don't
    understand why your form is getting submitted when someone presses the
    return key. Is this a self processing form?

    http://askblax.com


    please-answer-here wrote:
    > Scenario:
    >
    > I'm using the following code to avoid that users use the return key in the
    > middle of a form to submit the entire form
    > ---- snip ---------
    > function kH(e) {
    > var pK = document.all? window.event.keyCode:e.which;
    > return pK != 13;
    > }
    >
    > document.onkeypress = kH;
    > if (document.layers) document.captureEvents(Event.KEYPRESS);
    > ---- snip ---------
    >
    > What I would do is to modify the code so that when the users is in a
    > textarea input field they are allowed to use the return key. Is there a
    > funcion/method I can use in the function to test in which named input field
    > is the active one?
     
    askMe, May 26, 2005
    #2
    1. Advertising

  3. askMe wrote:
    > Curious. Why aren't you using an onsubmit event? Or are you? I
    > don't understand why your form is getting submitted when someone
    > presses the return key. Is this a self processing form?


    I don't know neither. The code is inherited. Here is a stripped version:
    ---[snip]---

    <%
    response.write("<html><head>")
    response.write("<title>Testing</title>")
    %>

    <script type="text/javascript">
    function kH(e) {
    var pK = window.event.keyCode;
    return pK != 13;
    }
    document.onkeypress = kH;
    </script>

    <%
    response.write("</head>")
    response.write("<body>")
    response.write("<form action=""showmodel.asp"" name=""dummy"" >")
    response.write("<br><input name=""a"" type=""text"" autocomplete=""off"" >")
    response.write("<br><input name=""b"" type=""text"" autocomplete=""off"" >")
    response.write("<p><input name=""c"" type=""submit"" >")
    response.write("</form>")
    response.write("</body>")
    response.write("</html>")
    %>

    Without the onkeypress handler the form gets submitted when i press return
    in any of the inupt fields

    >
    > http://askblax.com
    >
    >
    > please-answer-here wrote:
    >> Scenario:
    >>
    >> I'm using the following code to avoid that users use the return key
    >> in the middle of a form to submit the entire form
    >> ---- snip ---------
    >> function kH(e) {
    >> var pK = document.all? window.event.keyCode:e.which;
    >> return pK != 13;
    >> }
    >>
    >> document.onkeypress = kH;
    >> if (document.layers) document.captureEvents(Event.KEYPRESS);
    >> ---- snip ---------
    >>
    >> What I would do is to modify the code so that when the users is in a
    >> textarea input field they are allowed to use the return key. Is
    >> there a funcion/method I can use in the function to test in which
    >> named input field is the active one?
     
    please-answer-here, May 26, 2005
    #3
  4. please-answer-here

    Sandfordc Guest

    Here I have a simple fix for you,

    <script>
    function check(cde){
    if(cde == 13){
    event.returnValue=false
    }
    }
    </script>

    <body onkeydown="check(event.keyCode)">

    This will do the trick, but remember it will TOTALLY disable the enter
    key like previously sugessted maybe try,

    <form onsubmit="check(event.keyCode)">

    Then you have to submit the form by clicking the submit button.

    Hope this helped.

    Best Regards,
    Sandfordc
    http://www.javascript-central.tk
     
    Sandfordc, May 27, 2005
    #4
  5. Sandfordc wrote:
    > Here I have a simple fix for you,
    >
    > <script>
    > function check(cde){
    > if(cde == 13){
    > event.returnValue=false
    > }
    > }
    > </script>
    >
    > <body onkeydown="check(event.keyCode)">
    >
    > This will do the trick, but remember it will TOTALLY disable the enter
    > key like previously sugessted maybe try,


    Hey
    Thanks for the advice.
    BUT - I'm not having any problems in disabling the return key. Works fine
    with my own code. What I would like to implement is that I want to modify
    this code, so that when a user is in a textarea they are allowed to use the
    return key. So my question is:

    can I modify my code or check() above so I can check in which (input) field
    the cursor is placed when the key is pressed?

    >
    > <form onsubmit="check(event.keyCode)">
    >
    > Then you have to submit the form by clicking the submit button.
    >
    > Hope this helped.
    >
    > Best Regards,
    > Sandfordc
    > http://www.javascript-central.tk
     
    please-answer-here, May 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page