From this @ Merlyn Morgan-Graham solution here How can I use jQuery to reassign the tab-order from horizontal to vertical in a table?
and @dekel solution here, I came up with the below answer. Which works for most part. enter key to follow the tabindex (in scenario where enter key is changed to behave as tab)
function assignTabIndex() {
// Define variables
var maxRowCount = 0;
// Loop through all rows and find the children (td) length
// Get the maxRowCount
$('table tbody tr').each(function() {
maxRowCount = Math.max(maxRowCount, $(this).children('td').length);
});
// Define and start the cell count at 1
var cellCounter = 1;
// Loop through the table, column first instead of row first
for (var columnIndex = 0; columnIndex < maxRowCount; ++columnIndex) {
// Loop through all the rows for the current column
$('form table tr').each(function() {
// ...but only look at cells matching the current column
var cellForCurrentColumn = $(this)
.children('td')
.eq(columnIndex)
.find('input[type =text]:not([disabled])');
// Some rows could be bigger than others,
// so cellForCurrentColumn might not exist for shorter rows
if (cellForCurrentColumn != null) {
// Set the tab index and then increment the counter
cellForCurrentColumn.attr('tabindex', cellCounter++);
}
});
}
};
// Enter key to follow tab index
function EnterKeyAsTabKey() {
$(document).ready(function() {
assignTabIndex(); //call the assignTabIndex 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', and not disabled
// 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 (event.which === 13 && this.type !== 'submit' || 'reset') {
evt.preventDefault();
// Get the attribute type and if the type is not submit
itype = $(this).prop('type');
// Get the current Tabindex
currentTabindex = $(this).attr('tabindex');
// alert(currentTabindex); // alert to check the value a variable has. Good for trouble shoot
if (currentTabindex) {
nextInput = $('input[tabindex^="' + (parseInt(currentTabindex) + 1) + '"]');
// console.log(this, nextInput); // to print next input in console. Good for trouble shoot
if (nextInput.length) {
nextInput.focus();
} else
return false;
}
}
});
})
};