I am trying to create a function that when key press Enter occurs the next input with a class is selected.
I have managed to .focus() the next element within the same row. However, if I need to select the next input on the following row it fails to proceed.
The first .quantity textbox in the next row needs to be focused.
There is no error in console.
HTML
<table>
<tr>
<td>
<input class='quantity' value=''/>
<input class='100' value=''/>
</td>
<td>
<input class='quantity' value=''/>
<input class='50' value=''/>
</td>
<td>
<input class='quantity' value=''/>
<input class='20' value=''/>
</td>
</tr>
<tr>
<td>
<input class='quantity' value=''/>
<input class='100' value=''/>
</td>
<td>
<input class='quantity' value=''/>
<input class='50' value=''/>
</td>
<td>
<input class='quantity' value=''/>
<input class='20' value=''/>
</td>
</tr>
</table>
JQuery
$(".quantity").on('keypress', function (e) {
if (e.keyCode === 13) {
$(this).parent().next('td').find('input.quantity').focus();
}
});
You can use
index
method:http://jsfiddle.net/cwgZP/
Or if you want to select all the inputs you can try: