retrieving current input field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • please-answer-here

    retrieving current input field

    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.ke yCode:e.which;
    return pK != 13;
    }

    document.onkeyp ress = kH;
    if (document.layer s) document.captur eEvents(Event.K EYPRESS);
    ---- 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

    #2
    Re: retrieving current input field

    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?




    please-answer-here wrote:[color=blue]
    > 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.ke yCode:e.which;
    > return pK != 13;
    > }
    >
    > document.onkeyp ress = kH;
    > if (document.layer s) document.captur eEvents(Event.K EYPRESS);
    > ---- 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?[/color]

    Comment

    • please-answer-here

      #3
      Re: retrieving current input field

      askMe wrote:[color=blue]
      > 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?[/color]

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

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

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

      <%
      response.write( "</head>")
      response.write( "<body>")
      response.write( "<form action=""showmo del.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
      [color=blue]
      >
      > http://askblax.com
      >
      >
      > please-answer-here wrote:[color=green]
      >> 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.ke yCode:e.which;
      >> return pK != 13;
      >> }
      >>
      >> document.onkeyp ress = kH;
      >> if (document.layer s) document.captur eEvents(Event.K EYPRESS);
      >> ---- 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?[/color][/color]


      Comment

      • Sandfordc

        #4
        Re: retrieving current input field

        Here I have a simple fix for you,

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

        <body onkeydown="chec k(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


        Comment

        • please-answer-here

          #5
          Re: retrieving current input field

          Sandfordc wrote:[color=blue]
          > Here I have a simple fix for you,
          >
          > <script>
          > function check(cde){
          > if(cde == 13){
          > event.returnVal ue=false
          > }
          > }
          > </script>
          >
          > <body onkeydown="chec k(event.keyCode )">
          >
          > This will do the trick, but remember it will TOTALLY disable the enter
          > key like previously sugessted maybe try,[/color]

          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?
          [color=blue]
          >
          > <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[/color]


          Comment

          Working...