I have a bootstrap form where I want to set the focus to the next 'enabled' input element upon pressing enter key. When I press enter in Barcode input element, I want to set the focus to the Quantity input element since it is the next enabled input element.
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class="col-3 pr-0"> <div class="form-group"> <label for="txtBarcode">Barcode</label> <input type="text" id="txtBarcode" name="barcode" class="form-control form-control-sm"> </div> </div> <div class="col-7 pl-2 pr-0"> <div class="form-group"> <label for="txtPartDesc">Description</label> <input type="text" id="txtPartDesc" name="part_desc" value="" class="form-control form-control-sm" disabled> </div> </div> <div class="col-2 pl-2"> <div class="form-group"> <label for="txtUom">UoM</label> <input type="text" id="txtUom" name="barcode" value="" class="form-control form-control-sm" disabled> </div> </div> </div> <div class="row"> <div class="col-4 pr-0"> <div class="form-group"> <label for="txtQuantity">Quantity</label> <input type="text" id="txtQuantity" name="barcode" class="form-control form-control-sm"> </div> </div> </div> What I have tried so far is:
$(":input").keydown(function(event){ if (event.keyCode === 13) { $(this).nextAll(':input:enabled').first().focus(); } }); But this doesn't work as I expect.
.nextAllonly looks at siblings, not the whole HTML