I have a form with multiple buttons (used to verify each section is complete before making the next section visible and alter the next section based on previous responses). I have also hidden the final submit button until a final verification is done (re-runs all the individual verifications). This way the user can't click to submit until all required fields are complete. However the enter key is still bound to the submit button. I have looked and found some event handler code that will turn off the enter key function(which I don't understand very well) that would prevent early submission of the form, but I would rather tweak that code to transfer the enter key function to each button successively and then finally to the submit button itself. Any ideas?
The example code I found that I can't make sense of:
<form>
<label for="age">Age:</label>
<input type="number" min="0" max="120" name="age" id="age">
<button id="child">Child</button>
<button id="adult">Adult</button>
</form>
<script>
(function() {
var age = document.getElementById('age');
age.addEventListener('keypress', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
if (age.value > 20) {
document.getElementById('adult').click();
} else {
document.getElementById('child').click();
}
}
});
}());
</script>
Ended up mashing a bunch of suggestions I got here together along with some further research that it pointed to.
started out with this at the head of my
Then added a portion of it to each of my verifications for null inputs: