Skip to content

Instantly share code, notes, and snippets.

@jashezan
Created May 16, 2023 18:11
Show Gist options
  • Save jashezan/a4582cd67407f5ff29db8ddb24795cdf to your computer and use it in GitHub Desktop.
Save jashezan/a4582cd67407f5ff29db8ddb24795cdf to your computer and use it in GitHub Desktop.
On Enter keypress, I want to go to next input field
<!DOCTYPE html>
<html>
<head>
<title>Focus Next Input Field on Enter</title>
</head>
<body>
<input type="number" class="input-field" />
<input type="number" class="input-field" />
<input type="number" class="input-field" />
<script>
const inputFields = document.querySelectorAll('.input-field');
inputFields.forEach((input, index) => {
input.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
event.preventDefault();
const nextIndex = index + 1;
if (nextIndex < inputFields.length) {
inputFields[nextIndex].focus();
}
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment