Its a simple form with a action that should go to the “nextPage.html”. it only has 1 input which is a checkbox and a submit button.
JavaScript
x
13
13
1
const form = document.getElementById('form')
2
const checkbox = document.getElementById('checkbox')
3
const message = document.getElementById('message')
4
5
form.addEventListener('submit', function validateCheckbox(e) {
6
e.preventDefault()
7
if (checkbox.checked === true) {
8
message.innerHTML = 'Done'
9
}
10
else {
11
message.innerHTML = 'Please check the box to continue!'
12
}
13
})
JavaScript
1
9
1
<div class="container">
2
<form method="post" action="nextPage.html" id="form">
3
<label for="checkbox">By continuing, I agree that I have read and understand the terms of service.</label> <br>
4
<input type="checkbox" id="checkbox">
5
<button type="submit">Submit</button>
6
<h5 id="message"></h5>
7
</form>
8
9
</div>
Advertisement
Answer
e.currentTarget.submit();
will “manually” submit the form after you’ve verified that the checkbox is checked. Also side-note, it is always recommended to include semicolon line terminations.
JavaScript
1
13
13
1
const form = document.getElementById('form');
2
const checkbox = document.getElementById('checkbox');
3
const message = document.getElementById('message');
4
5
form.addEventListener('submit', function validateCheckbox(e) {
6
e.preventDefault();
7
if (checkbox.checked === true) {
8
message.innerHTML = 'Done';
9
e.currentTarget.submit();
10
} else {
11
message.innerHTML = 'Please check the box to continue!';
12
}
13
})
JavaScript
1
8
1
<div class="container">
2
<form method="post" action="nextPage.html" id="form">
3
<label for="checkbox">By continuing, I agree that I have read and understand the terms of service.</label> <br>
4
<input type="checkbox" id="checkbox">
5
<button type="submit">Submit</button>
6
<h5 id="message"></h5>
7
</form>
8
</div>