In an HTML table a reasonable number of cells, spanning numerous rows, include text inputs that may be enabled or not depending on prior selected options in dropdowns. While the tabindex is correctly configured so that users can use TAB or SHIFT+TAB to navigate between these inputs, I have been asked to replicate this behavior using the LEFT and RIGHT arrow keys, which is proving rather difficult to implement.
Simplified version of HTML:
<html><body>
<table><tr>
<th></th>
<th>Sample<br/>01</th>
<th>Sample<br/>02</th>
<th>Sample<br/>03</th>
<th>Sample<br/>04</th>
<th>Sample<br/>05</th>
</tr><tr>
<th>Group 01</th>
<td>
<input type="text" tabindex="1" class="sample-value"
name="edtG01S01" id="edtG01S01"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S01" id="cbxG01S01"/> <1
</td>
<td>
<input type="text" tabindex="2" class="sample-value"
name="edtG01S02" id="edtG01S02"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S02" id="cbxG01S02"/> <1
</td>
<td>
<input type="text" tabindex="3" class="sample-value"
name="edtG01S03" id="edtG01S03" disabled="disabled"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S03" id="cbxG01S03"/> <1
</td>
<td>
<input type="text" tabindex="4" class="sample-value"
name="edtG01S04" id="edtG01S04"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S04" id="cbxG01S04"/> <1
</td>
<td>
<input type="text" tabindex="5" class="sample-value"
name="edtG01S05" id="edtG01S05"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S05" id="cbxG01S05"/> <1
</td>
</tr><tr>
<th>Group 02</th>
<td>
<input type="text" tabindex="6" class="sample-value"
name="edtG02S01" id="edtG02S01"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S01" id="cbxG02S01"/> <1
</td>
<td>
<input type="text" tabindex="7" class="sample-value"
name="edtG02S02" id="edtG02S02"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S02" id="cbxG02S02"/> <1
</td>
<td>
<input type="text" tabindex="8" class="sample-value"
name="edtG02S03" id="edtG02S03" disabled="disabled"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S03" id="cbxG02S03"/> <1
</td>
<td>
<input type="text" tabindex="9" class="sample-value"
name="edtG02S04" id="edtG01S04"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S04" id="cbxG02S04"/> <1
</td>
<td>
<input type="text" tabindex="10" class="sample-value"
name="edtG02S05" id="edtG02S05"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S05" id="cbxG02S05"/> <1
</td>
</tr></table>
</body></html>
The inputs have the class sample-value
, and at the moment the closest I've got working is:
$('input.sample-value').keyup(function(event) {
if (event.which == 39) { /* right arrow key */
$(this).parent('td').next('td').find('input.sample-value').focus();
event.preventDefault();
}
if (event.which == 37) { /* left arrow key */
$(this).parent('td').prev('td').find('input.sample-value').focus();
event.preventDefault();
}
});
With the above code the left and right arrow keys navigate between inputs as expected, until you come to a disabled input, or until you come to the end of a row. At that point it is unable to skip forward or back to the next focusable input.
Is there a way this code can be adapted to be flexible enough to navigate to the appropriate input.sample-value
element regardless of where it sits in table rows or columns, and being able to take into account skipping disabled inputs? The inputs are not disabled at the time of page load but dynamically enabled or disabled in response to selected options in dropdowns preceeding these inputs.
Full working example:
$('input.sample-value').keyup(function(event) {
if (event.which == 39) { /* right arrow key */
$(this).parent('td').next('td').find('input.sample-value').focus();
event.preventDefault();
}
if (event.which == 37) { /* left arrow key */
$(this).parent('td').prev('td').find('input.sample-value').focus();
event.preventDefault();
}
});
* {
font-family: Arial;
}
input.sample-value {
width: 50px;
}
th, td {
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html><body>
<table><tr>
<th></th>
<th>Sample<br/>01</th>
<th>Sample<br/>02</th>
<th>Sample<br/>03</th>
<th>Sample<br/>04</th>
<th>Sample<br/>05</th>
</tr><tr>
<th>Group 01</th>
<td>
<input type="text" tabindex="1" class="sample-value"
name="edtG01S01" id="edtG01S01"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S01" id="cbxG01S01"/> <1
</td>
<td>
<input type="text" tabindex="2" class="sample-value"
name="edtG01S02" id="edtG01S02"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S02" id="cbxG01S02"/> <1
</td>
<td>
<input type="text" tabindex="3" class="sample-value"
name="edtG01S03" id="edtG01S03" disabled="disabled"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S03" id="cbxG01S03"/> <1
</td>
<td>
<input type="text" tabindex="4" class="sample-value"
name="edtG01S04" id="edtG01S04"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S04" id="cbxG01S04"/> <1
</td>
<td>
<input type="text" tabindex="5" class="sample-value"
name="edtG01S05" id="edtG01S05"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG01S05" id="cbxG01S05"/> <1
</td>
</tr><tr>
<th>Group 02</th>
<td>
<input type="text" tabindex="6" class="sample-value"
name="edtG02S01" id="edtG02S01"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S01" id="cbxG02S01"/> <1
</td>
<td>
<input type="text" tabindex="7" class="sample-value"
name="edtG02S02" id="edtG02S02"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S02" id="cbxG02S02"/> <1
</td>
<td>
<input type="text" tabindex="8" class="sample-value"
name="edtG02S03" id="edtG02S03" disabled="disabled"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S03" id="cbxG02S03"/> <1
</td>
<td>
<input type="text" tabindex="9" class="sample-value"
name="edtG02S04" id="edtG01S04"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S04" id="cbxG02S04"/> <1
</td>
<td>
<input type="text" tabindex="10" class="sample-value"
name="edtG02S05" id="edtG02S05"/>
<br/>
<input type="checkbox" tabindex="-1" name="cbxG02S05" id="cbxG02S05"/> <1
</td>
</tr></table>
</body></html>
Also available as a JSFiddle: https://jsfiddle.net/zoLwv5d9/