How to set focus to next input field in a table?
Firstly I worked with that JQuery code: But it only works if the input fields are not spread over different and fields. How could I improve it that code or maybe someone knows a JS code that will work for autofocus fields spread over td and tr fields following tabindex?
<script>
$(document).ready(function(){
$('input').keyup(function(){
if(this.value.length==$(this).attr("maxlength")){
$(this).next().focus();
}
});
});
</script>
My html:
<table>
<tr>
<td><input type="text" id="field1" tabindex="1" name="test1" maxlength = "1"></td>
<td><input type="text" id="field2" tabindex="2" name="test2" maxlength = "1">.</td>
</tr>
<tr><td><input type="text" id="field3" tabindex="3" name="test3" maxlength = "1"></td></tr>
</table>
2
Answers
To do what you require you can retrieve all
input
elements and get theindex
of the current one within that collection. To move to the next input, select it from the collection usingeq()
and the next incremental index value.Also note that your HTML is invalid – you seem to be missing a
<tr>
around the finaltd
. I’ve corrected that in the following example:It’s pretty gross, but would work lol