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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Focus on a field after a keypress event?

    I'm having some basic trouble with focusing on a particular field after carrying out some keypress events.

    Basically I have a barcode scanner and three input fields. This scanner inputs the text it scans then tabs. Currently the setup is that it tabs from field1 to field2 to field3 and on tab on field3 it presses a button to submit the form.

    All I want it to do after that submit is to now set focus back on field one, so that I can scan with my barcode scanner again, but it doesn't seem to be working. I'm hazarding a guess that it might be something to do with the sequence instructions are happening in.

    JS Code

    This line in particular:
    <code>$("#testform [name='OrderNo']").focus();</code>
    I've tried a few examples of doing this around the internet but either way it doesn't seem to be doing anything at all. This is what I need fixing.

    <code>
    $(document).ready(function() {

    //attach keypress event listener to the CompletedBy field
    $('#CompletedBy').keydown(function(event){
    //Check if the key pressed is TAB key using the keyCode=9
    if(event.keyCode === 9){
    $( "#submit" ).click();
    $("#testform [name='OrderNo']").focus();
    }
    event.cancelBubble = true;
    if (event.stopPropagation) event.stopPropagation();
    });

    });
    </code>


    HTML Code
    <code>

    <table class="insert">
    <form name="myform" id="testform" method="post" action="insert.php">
    <tr class="insert">
    <td class="insert">
    <div class="insert">OrderNo</div>
    <input class="insert" id="orderno" type="text" tabindex=1 name="OrderNo">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <div class="insert">New Status</div>
    <input class="insert" type="text" tabindex=2 name="NewStatus">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <div class="insert">Completed by</div>
    <input class="insert" Name="CompletedBy" id="CompletedBy" tabindex=3 type="text" name="CompletedBy">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <input class="insertbutton" type="submit" id="submit" name="submit" value="Enter Record">
    </tr>
    </td>
    </table>
    </td>
    </tr>
    </form>
    </table>
    </code>

    Thank you.

  • #2
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by adamantium42 View Post
    I'm having some basic trouble with focusing on a particular field after carrying out some keypress events.

    Basically I have a barcode scanner and three input fields. This scanner inputs the text it scans then tabs. Currently the setup is that it tabs from field1 to field2 to field3 and on tab on field3 it presses a button to submit the form.

    All I want it to do after that submit is to now set focus back on field one, so that I can scan with my barcode scanner again, but it doesn't seem to be working. I'm hazarding a guess that it might be something to do with the sequence instructions are happening in.

    JS Code

    This line in particular:
    Code:
    $("#testform [name='OrderNo']").focus();
    I've tried a few examples of doing this around the internet but either way it doesn't seem to be doing anything at all. This is what I need fixing.

    Code:
            $(document).ready(function() {
              
              //attach keypress event listener to the CompletedBy field
             $('#CompletedBy').keydown(function(event){
              //Check if the key pressed is TAB key using the keyCode=9
              if(event.keyCode === 9){
    $( "#submit" ).click();
    $("#testform [name='OrderNo']").focus();
              }
              event.cancelBubble = true;
                 if (event.stopPropagation) event.stopPropagation();
             });
               
            });

    HTML Code
    Code:
    <table class="insert">
    <form name="myform" id="testform" method="post" action="insert.php">
    <tr class="insert">
    <td class="insert">
    <div class="insert">OrderNo</div>
    <input class="insert" id="orderno" type="text" tabindex=1 name="OrderNo">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <div class="insert">New Status</div>
    <input class="insert" type="text" tabindex=2 name="NewStatus">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <div class="insert">Completed by</div>
    <input class="insert" Name="CompletedBy" id="CompletedBy" tabindex=3 type="text" name="CompletedBy">
    </tr>
    </td>
    <tr class="insert">
    <td class="insert">
    <input class="insertbutton" type="submit" id="submit" name="submit" value="Enter Record">
    </tr>
    </td>
    </table>
    </td>
    </tr>
    </form>
    </table>
    Thank you.
    Apologies.. proper code tags are in now.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Place the focus on the field before the submit.

    Code:
    if(event.keyCode === 9){
    $("#testform [name='OrderNo']").focus();
    $( "#submit" ).click();
     }

    He wouldn't want to be a camel and bury his head in the sand. - Commentator BT Sport

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Place the focus on the field before the submit.

    Code:
    if(event.keyCode === 9){
    $("#testform [name='OrderNo']").focus();
    $( "#submit" ).click();
     }

    He wouldn't want to be a camel and bury his head in the sand. - Commentator BT Sport
    No joy unfortunately

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    submitting the form reloads the page, making everything lose focus, no? So don't you just need to focus on that field as soon as the page loads?
    Code:
    $(document).ready(function() {
    $("#testform [name='OrderNo']").focus();
    
     //attach keypress event listener to the CompletedBy field
             $('#CompletedBy').keydown(function(event){
    //etc


  •  

    Posting Permissions

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