In this video we will learn how to detect changes in checkbox field and act on it accordingly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox JS events</title>
</head>
<body>
<h1>Register account here</h1>
<form action="">
<fieldset>
<legend>Fill in the details</legend>
<p>Username: <input type="text"></p>
<p>Name: <input type="text"></p>
<p>Password: <input type="password"></p>
<p>Confirm Password: <input type="password"></p>
<p><input type="checkbox" id="agree">
<label for="agree">I agree to terms and conditions</label>
</p>
<input type="submit" value="Register" id="register" disabled>
</fieldset>
</form>
<script>
let agree = document.getElementById("agree");
agree.addEventListener('change', function() {
let register = document.getElementById("register");
if(agree.checked) {
register.removeAttribute("disabled");
} else {
register.setAttribute("disabled", "disabled");
}
});
</script>
</body>
</html>