Created
May 16, 2023 18:11
-
-
Save jashezan/a4582cd67407f5ff29db8ddb24795cdf to your computer and use it in GitHub Desktop.
On Enter keypress, I want to go to next input field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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