javascript - Unable to set focus to the next text box -
i have 3 text boxes class "initialbox".
the max length each box 1 character. when box has been filled 1 character, next box should set focus. (the exact same way using tab key work).
i added alert() in order check event if firing, , is, $(this).next('.initialbox').focus() doesn't set focus next tab. have tried using $(this).next.focus().
many thanks.
html:
<table style="width:100%;"> <tr> <td style="width:33.3%"> <input type="text" class="initialbox" maxlength="1" size="1" id="firstinitial" /> </td> <td style="width:33.3%"> <input type="text" class="initialbox" maxlength="1" size="1" id="secondinitial" /> </td> <td style="width:33.3%"> <input type="text" class="initialbox" maxlength="1" size="1" id="thirdinitial" /> </td> </tr> <tr> <td style="width:33.3%">1st initial</td> <td style="width:33.3%">2nd initial</td> <td style="width:33.3%">3rd initial</td> </tr> </table> jquery:
$(".initialbox").keyup(function () { if (this.value.length == 1) { alert(); $(this).next('.initialbox').focus(); } }); jsfiddle: http://jsfiddle.net/v9y51pds/1/
you can't use .next() markup, because .initialbox input elements aren't siblings.
you need traverse parent (closest()) <td>, along it's neighbour <td> , find .initialbox within:
$(".initialbox").keyup(function () { if (this.value.length == 1) { alert(); $(this).closest('td').next('td').find('.initialbox').focus(); } });
Comments
Post a Comment