Answers for "javascript add click event"

47

javascript onclick event listener

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

how to add onclick event in javascript

var element = document.getElementById("elem");
element.onclick = function(event) {
  console.log(event);
}
Posted by: Guest on May-20-2020
2

js addeventlistener click

document.getElementById("ELEMENT_ID").addEventListener("click", function() {
  //insert here the code to be executed
});
Posted by: Guest on July-22-2021
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 add click event"

Code answers related to "Javascript"

Browse Popular Code Answers by Language