Answers for "javascript function click event listener"

47

js add click listener

document.getElementById("myBtn").addEventListener("click", function() {
  alert("Hello World!");
});
Posted by: Guest on July-10-2020
0

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
Posted by: Guest on August-18-2021

Code answers related to "javascript function click event listener"

Code answers related to "Javascript"

Browse Popular Code Answers by Language