Currently when the user presses tab in the qty text box it does not tab to the add line image.
I want to add some jQuery in which would make it so that when the user presses the tab key within the qty text box it tabs to the add image where the user can click enter.
<td>
<input type="text" name="qty" id="qty" maxlength="3" class="txt" style="width: 80px;" />
</td>
<td>
<input type="image" src="/gfx/btn_addline.jpg" name="Add" value="Add" id="add"/>
</td>
Jquery affecting qty:
$("#qty").keydown(function (event) {
//alert(event.keyCode);
if (event.keyCode == 13) {
$("#add").click();
return false;
}
});
$("#qty").keydown(function (event) {
// Allow only backspace and delete
if (event.keyCode == 46 || event.keyCode == 8) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
Assuming I’ve understood your question correctly, you should just use the
tabindexattribute, which doesn’t require any JavaScript:Update (see comments)
If you already use
tabindexto control tab order elsewhere on your page, just use higher indexes here. For example, if indexes 1-10 are used on other elements, use 11 and 12 here:Update 2 (see updated question)
You are preventing the tab key from doing anything in your
keydownevent handler. Change to the following (add another condition that checks for the tab key):This will work without the need for
tabindex, assuming your two inputs are in order in your code and there are no inputs between them. If there are, usetabindexas well.As a side note, you should use
event.whichinstead ofkeyCode, since jQuery uses it to normalise browser inconsistencies.