Here is a code that I am using in my current project. There are two fields to enter numbers each allows max 3 digits. I would like to automatically switch to second text field on completing first field with 3 digits. How can I do that ?
I found lots of example for input fields but as here I am using <td>
I couldn't find a solution myself.
https://jsfiddle.net/shijilt/54918g3s/
<div class="content">
<div class="content mb-0 mt-3">
<div class="row mb-0">
<div id="add-product">
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><h6 class="font-14"><strong>Number</strong></h6></th>
<th><h6 class="font-14"><strong>Count</strong></h6></th>
</tr>
<tr>
<td contentEditable="true" inputmode="numeric" data-id="lot_number_3" data-length="3" class="test"><h3 class="color-green-dark mb-0"></h3></td>
<td contentEditable="true" inputmode="numeric" data-id="lot_count_3"data-length="3" class="test"><h3 class="color-red-dark mb-0">1</h3></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$('td[contentEditable="true"]').on("keydown", e => {
// define allowed keys
const allow = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Tab', 'Backspace']
// get pressed key
const k = e.key
if (allow.includes(k)) {
// get the value before this keydown
const val = e.target.textContent
// determine the limit according to your logic
const limit = Number(e.target.dataset['length']);
if (val.length == limit && 'Tab' != k && 'Backspace' != k) {
// pressing this key would put the value over the limit
e.preventDefault()
console.log('deny: ' + k, val, limit)
} else
console.log('allow: ' + k, val, limit)
} else {
// key not allowed
e.preventDefault()
console.log('deny: ' + k)
}
})
</script>