javascript onclick event listener
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
javascript onclick event listener
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
how to add onclick event in javascript
var element = document.getElementById("elem");
element.onclick = function(event) {
console.log(event);
}
js addeventlistener click
document.getElementById("ELEMENT_ID").addEventListener("click", function() {
//insert here the code to be executed
});
js addeventlistener click
// html
<!DOCTYPE html>
<html>
<head>
<title>Test HTML</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form method="post">
<div>
<label for="terms_and_conditions">I agree to the Terms and Conditions:</label>
<input type="checkbox" id="terms_and_conditions" value="1" />
</div>
<div>
<button type="submit" id="submit_button" disabled>Sign Up</button>
</div>
</form>
<script src="test.js"></script>
</body>
</html>
// jQuery option - test.js
$("#terms_and_conditions").click(function () {
//If the checkbox is checked.
if ($(this).is(":checked")) {
//Enable the submit button.
$("#submit_button").attr("disabled", false);
} else {
//If it is not checked, disable the button.
$("#submit_button").attr("disabled", true);
}
});
//js options - test.js
let privacyCheck = document.querySelector("#terms_and_conditions");
privacyCheck.addEventListener("click", function () {
if (privacyCheck.checked) {
document.querySelector("#submit_button").disabled = false;
} else {
document.querySelector("#submit_button").disabled = true;
}
});
// if you decide to use the js version remove jQuery from the header to avoid confusion
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us