1

I have an enter key that acts as a tab. It works on my other textbox, but after i reached the table data, it doesn't work anymore. My target is how can I go to the next textbox after I press the "enter" key on my table data?

enter image description here

enter image description here

//Enter key that acts as a tab key.
    $('input').keypress(function(e){
        if(e.which==13){ 
            $("[tabindex='"+(parseInt($(this).attr("tabindex"))+1)+"']").focus();
            e.preventDefault();
        }
    });   

//This function creates a tabindex for every textbox.
$(function() {
    var tabindex = 1;
    $('input,select').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });
});

   //This function appends textbox. It depends on my dropdown value on my previous page. If user selects "2", on the next page, it will create 2 set of rows with textbox
$(document).ready(function(){

    samples();
  function samples(){
  
 
       $('.appendtablee').html('');
       
     
    for (var i = 0; i<parseInt($('#asdasdas').val()); i++) {
        var row = '<tr>\
            <td>\
                <label> </label>\
            </td>\
            <td>\
                <input type="text" name="l[]" class="form" id="legBand1" />        \
                 <span class="email_result"></span> \
            </td>\
            <td>\
                <input type="text" data-id="wp"  name="W[]" min="1700" max="2200" />        \
            </td>\
            <td>\
                <input type="text" name="Wi[]"  />        \
            </td>\
        </tr>';
        row = $(row);
        $('.appendtablee').append(row);
    }
  
  }

})  

  

//When i press F8 key, it adds row.
    $(window).keydown(function(){
        if (event.keyCode == 119) {
            var row = "<tr><td></td><td><input type='text' tabindex='0' class='form myInput' name='legBand[]' id='legBand1' /> <span class='email_result'></span></td><td><input type='text' class='myInput' data-id='weight'  name='Weight[]' min='1700' max='2200' /></td><td><input type='text' class='myInput' name='Wingband[]' /></td><td><button type='button' style='float:left;' class='removerow btn btn-danger'>X REMOVE ENTRY</button></td></tr>";
            $("#wew").append(row);
            }
        });
    
    $("#wew").on("click", ".removerow", function() {
       $(this).closest("tr").remove(); 
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" border="1" class="classtable" id="applicanttable">
     
     
     <tr>
                <th>#</th>
                <th>B#</th>
                <th>W#</th>
                <th>W #</th>
                <th>Action</th>
             </tr>
     
            <tbody class="appendtablee" id="wew">
            
                   
          </tbody>
  • You've include an `tbody` without `tr` - do you load the table after the page loads? You've *explicitly* put your add-tabindex code inside doc.ready, but have not put (in the question) the `$('input')` code - is your `$("input")` code running before you load the table? Can you create a *complete* snippet that *demonstrates* the issue?(shouldn't be hard as most of it's there already, just need some inputs and some table rows/inputs) - [edit] and click `[<>]`. – freedomn-m Nov 22 '21 at 09:01
  • @Ulquiorra Schiffer: Could you share your code snippet for us to have a look :) – Imran Rafiq Rather Nov 22 '21 at 09:06
  • @freedomn-m Yes Mate :) That is always the best option. Thank you :) – Imran Rafiq Rather Nov 22 '21 at 09:08
  • Extrapolating some html, your code [works fine](https://jsfiddle.net/nsrx3p6y/) so there must be some other reason, eg html loaded after the code has run. – freedomn-m Nov 22 '21 at 09:10
  • I'm sorry, yes. On my previous page, the user has a choice to select a dropdown value. After selecting a value, if the user selected "2", on the next page, it will create 2 sets of rows with textbox. – Ulquiorra Schiffer Nov 22 '21 at 10:38
  • Change `$('input').keypress(function(e){` to `$(document).on("keypress", "input", (function(e){` – freedomn-m Nov 22 '21 at 14:23
  • @freedomn-m Dear Friend, The reason that is given to close this question doesn't seem right. OP 's requirement is not to setup a event handler on the parent element (and use Event Bubbling concept- to handle the event from the specific target) . It is totally different from that. I would also like to learn from you if you kindly provide the reason to close it dear Friend :) Cheers !!! – Imran Rafiq Rather Nov 23 '21 at 07:12
  • @ImranRafiqRather OP is using `$('input').keypress` to handle the enter key. OP then, after this code runs, calls `samples()` which adds some more inputs. The enter key does not work *for these new inputs* - because they have been added dynamically. OP (after some time) has confirmed that they add the inputs after calling `$('input').keypress` - `$("input")` will only apply to elements *that exist at the time the call is made* - so the new inputs *will not* have the `keypress` event applied. – freedomn-m Nov 23 '21 at 09:23
  • @ImranRafiqRather So there are two solutions: one is to add the events after the elements have been added (which you do (using an unnecessary loop for some reason) in your answer) and the other is to use **event delegation**, which is designed *specifically* for this scenario. – freedomn-m Nov 23 '21 at 09:23
  • @ImranRafiqRather *OP 's requirement is not to setup a event handler on the parent element* - correct, OP's requirement is get the enter key to work and OPs **solution** (or one of, see above) is to set up an event handler on the/a parent element and use event binding. – freedomn-m Nov 23 '21 at 09:25

1 Answers1

1

UPDATED SOLUTION AFTER GETTING THE OP REQUIREMENT:

The Logic that I have figured out to traverse the input elements as required is as follows.

Just focus on your first input and then keep clicking Enter Key it will focus() on the next Element.

$(document).ready(function(){

    samples();
  function samples(){
  
 
       $('.appendtablee').html('');
       
     
    for (var i = 0; i<5; i++) { // I have used a hard coded value 5 for now (selector us missing)
        var row = '<tr>\
            <td>\
                <label> </label>\
            </td>\
            <td>\
                <input type="text" name="l[]" class="form myInput" id="legBand1" tabindex="0"/>        \
                 <span class="email_result"></span> \
            </td>\
            <td>\
                <input  class="myInput" type="text" data-id="wp"  name="W[]" min="1700" max="2200" tabindex="0"/>        \
            </td>\
            <td>\
            </td>\
        </tr>';
        row = $(row);
        $('.appendtablee').append(row);
    }
  
  
    const textFields = $('.myInput');
    for(let i=0;i<textFields.length;i++) {
        $(textFields[i]).on('keypress',function(e){
            if(e.which==13){ 
              $(textFields[i+1]).focus();
            }
        });   
     
    }
  }

})  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" border="1" class="classtable" id="applicanttable">
  <h1> Testing Testing </h1>
     
     <tr>
                <th>#</th>
                <th>B#</th>
                <th>W#</th>
                <th>W #</th>
                <th>Action</th>
             </tr>
     
            <tbody class="appendtablee" id="wew">
            
                   
          </tbody>

Please Note: I have not increased the tab-index value on each Keypress of Enter, I have avoided the mentioned logic, I don't think it was required in this case.

I have given tabindex=0 to all the inputs and also added a class myInput so that I can make use of that selector.

You can make use of this logic (If required make some changes if needed, but the functionality is working for inputs).

Codepen link if required: https://codepen.io/emmeiWhite/pen/QWMPRVy?editors=1111

Imran Rafiq Rather
  • 7,677
  • 1
  • 16
  • 35
  • OP's issue: *can't tab on fields in a table* - your solution: change the submit? Can't see how that helps the issue that OP has described. Also, if OP (*correctly*) uses a ` – freedomn-m Nov 22 '21 at 09:51
  • Hello, there's no problem in my submit. I have e.preventDefault() already. My question is how can i tab to the next textbox using my function which is enter key acting as a tab key. Thank you – Ulquiorra Schiffer Nov 22 '21 at 10:39
  • 1
    @freedomn-m Dear Friend, I just guessed a solution, since the code was not fully available. My assumption was may be the page is refreshing. Anyways. Thanks for your comment :) Let me have a look into the code. It seems the form is being created dynamically in Javascript ! This is very I can see some gotcha. Let me have a look first My Solution would be a bit different though: I wouldn't really pr – Imran Rafiq Rather Nov 22 '21 at 13:34
  • @UlquiorraSchiffer I have come up with a solution, May be this could help. The functionality is working fine. I did it without requiring two functions (for tabs and to select them). Give it a try. Cheers !!! – Imran Rafiq Rather Nov 22 '21 at 14:52
  • 1
    Thank you, works perfectly. Appreciated it and have a nice day. ^_^ – Ulquiorra Schiffer Nov 23 '21 at 03:33
  • My pleasure Friend @UlquiorraSchiffer Most welcome – Imran Rafiq Rather Nov 23 '21 at 07:07
  • Hello, is it okay to add a follow up question? I added an append. If i press F8 key, it will add row. My enter that acts like a tab key is not working there even tho i have class="myInput". Did i miss anything or what? Thank you in advance. https://prnt.sc/20lna6o I edited my post and added my code regarding the add row when F8 is pressed. – Ulquiorra Schiffer Nov 23 '21 at 07:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239470/discussion-between-imran-rafiq-rather-and-ulquiorra-schiffer). – Imran Rafiq Rather Nov 23 '21 at 07:19