1

I have a 3x3 table within a form. By default tab key moves the cursor in horizontal directions on these input fields. When a tabindex is given ( Like in the example below) the tab key moves cursor columns wise instead of row wise, as I needed.

With various sources from SO answers, I came up with the Jquery to use enter key as tab. But, could not figure out how to follow the tabindex as achieved above , i.e by pressing enter key instead of cursor moving row-wise, i want it to move in column wise. Below is what I have so far, any help is appreciated.

Demo of how it currently works. http://jsfiddle.net/unCu5/125/

Source of below code: jquery how to catch enter key and change event to tab

<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr>
</table>

$('input').live("keypress", function(e) {
  /* ENTER PRESSED*/
  if (e.keyCode == 13) {
    /* FOCUS ELEMENT */
    var inputs = $(this).parents("form").eq(0).find(":input");
    var idx = inputs.index(this);

    if (idx == inputs.length - 1) {
      inputs[0].select()
    } else {
      inputs[idx + 1].focus(); //  handles submit buttons
      inputs[idx + 1].select();
    }
    return false;
  }
})

@Dekel solution work for the html scenario he displayed, but I have a different type of HTML on view source. How do I fix this

Community
  • 1
  • 1
user5249203
  • 4,436
  • 1
  • 19
  • 45
  • Possible duplicate of [jquery how to catch enter key and change event to tab](http://stackoverflow.com/questions/2335553/jquery-how-to-catch-enter-key-and-change-event-to-tab) – Dekel Jul 20 '16 at 19:41
  • @Dekel, my Q is not how to change enter key to tab. I already solved it in the given code above. But, i want that enter key to follow tabindex provided to each input. By default, the enter key moves horizontally ( row-wise) for input fields ( let says for a 3x3 table), But, I want the cursor to move column-wise. So, how to change the default behaviour in JS here ? – user5249203 Jul 20 '16 at 19:48
  • Sorry, now it's more clear :) – Dekel Jul 20 '16 at 19:59
  • put jsfiddle.net DEMO , then , we can figure out yr pb – Abdennour TOUMI Jul 20 '16 at 19:59

1 Answers1

3

Instead of just focus the next input element, you can find the next element (based on the tabindex) and focus on him:

$('input[tabindex^="2"]');

Check this example:

$(document).ready(function () { // Will only run once the page Document Object Model (DOM) is ready for JavaScript code 
    // Create a jQuery object containing the html element 'input'
    // Create a .not() method to exclude buttons for keypress event
    $(":input:not(:disabled)").not($(":button")).keypress(function(evt) {
        // If the keypress event code is 13 (Enter)
        if (evt.keyCode == 13) {
            // get the attribute type and if the type is not submit
            itype = $(this).prop('type');
            if (itype !== 'submit') {
                currentTabindex = $(this).attr('tabindex');
                if (currentTabindex) {
                    nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                    if (nextInput.length) {
                        nextInput.focus();
                        return false;
                    }
                }
            }
        }
    });
});
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="4" placeholder="4" /></td>
        <td><input tabindex="7" placeholder="7" /></td>
    </tr><tr>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="5" placeholder="5" /></td>
        <td><input tabindex="8" placeholder="8" /></td>
    </tr><tr>
        <td><input tabindex="3" placeholder="3" /></td>
        <td><input tabindex="6" placeholder="6" /></td>
        <td><input tabindex="9" placeholder="9" /></td>
    </tr>
</table>

The code doesn't support go back from the last input to the first. You will need to write it explicitly.


Updated version - fix wrong tabindex values

The original question didn't mention that tabindex could repeat or don't have sequential values. This code will "fix" the values of tabindex based on the order in the code AND the values of the tabindex. It will support both repeated tabindex values and non sequential values (1, 2, 3, 6, 7):

function fixTabIndex() {
    // Find all inputs. Their order will be the same order they appear inside your html.
    inputs = $('input');
    // Save the original HTML order of the inputs (we need this to compare their order in the HTML in case or equal two tabindex 
    inputs_original = $('input');
    
    // Sort the inputs by their tabindex values and their position in the DOM
    // More info on Array.prototype.sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
    inputs.sort(function(a, b) {
        if ($(a).attr('tabindex') == $(b).attr('tabindex')) {
            // If tabindex is equal - sort by the position in the DOM
            if (inputs_original.index(a) < inputs_original.index(b)) {
                return -1;
            } else {
                return 1;
            }
        } else if ($(a).attr('tabindex') < $(b).attr('tabindex')) {
            return -1;
        } else {
            return 1;
        }
    });
    // Set the new value of the tabindex based on the position in the sorted array
    inputs.each(function(i, el) {
        $(el).attr('tabindex', i+1);
    });
}

$(document).ready(function () {
    // First we need to fix the tabindex values:
    fixTabIndex();
    $("input").keypress(function(evt) {
        // If the keypress event code is 13 (Enter)
        if (evt.keyCode == 13) {
            // Make sure this is not a submit input
            if ($(this).prop('type') !== 'submit') {
                currentTabindex = $(this).attr('tabindex');
                if (currentTabindex) {
                    nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                    if (nextInput.length) {
                        nextInput.focus();
                        return false;
                    }
                }
            }
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr>
</table>
Community
  • 1
  • 1
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • I tried your suggestion, what I noticed it by enter the cursor moves to the last row of second column in my original code. Need to figure out why. My table is inside a form, does that matter ? – user5249203 Jul 20 '16 at 20:18
  • You just didn't use the HTML I provided. Note the usage of the different `tabindex` in my table vs your table. – Dekel Jul 20 '16 at 20:19
  • @user5249203, did you run the code snippet from my answer? – Dekel Jul 20 '16 at 20:32
  • Are looking for LTR or for RTL solution? on my example when you put the cursor on the top-left input and click enter - the cursor will go to the middle-left input (the input that exists under last one). – Dekel Jul 20 '16 at 21:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117858/discussion-between-user5249203-and-dekel). – user5249203 Jul 20 '16 at 23:12
  • Can you add comments to explain the function. Today morning I tried implementing your function solution. It works column wise, but moves 2 columns ahead and then back on every enter key. Trying to solve it.. so if you can explain the code a bit I can fix it. – user5249203 Jul 22 '16 at 12:08
  • Added some comments and also fixed some bug in the sorting function. Check the new code there. – Dekel Jul 22 '16 at 16:02