- Joined
- Aug 16, 2022
- Messages
- 57
- Reaction score
- 2
Hi Everybody,
I have attached a short code block below.
What I want to know is: Where in this code is a "trigger" that causes the Javascript snippet
to execute?
::::
I have attached a short code block below.
What I want to know is: Where in this code is a "trigger" that causes the Javascript snippet
to execute?
HTML:
<!DOCTYPE HTML>
<html lang="en">
<head>
</head>
<body>
<input type="password" placeholder="Enter Your Password" id="PW1" name="PW1" required="required">
<br>
<input type="password" placeholder="Enter Your Password Again to Confirm" id="PW2" name="PW2" required="required"><br>
<!--Insert a script to evaluate "what to do" if PW1 is equal/unequal to PW2-->
<script>
// Find input elements with the ID (PW1, PW2)
const pw1 = document.querySelector('#PW1'),
pw2 = document.querySelector('#PW2');
let isPasswordsMatch = false; // variable called flag
// Use for both event: input
pw1.addEventListener('input', checkPasswordEquality);
pw2.addEventListener('input', checkPasswordEquality);
function checkPasswordEquality() {
// Remove leading and trailing whitespace from the password values
const password1 = pw1.value.trim();
const password2 = pw2.value.trim();
if (password1.length && password2.length) {
if (password1 === password2) {
// Passwords are the same
pw2.style.backgroundColor = '#00FF00';
isPasswordsMatch = true;
} else {
// Passwords are different
pw2.style.backgroundColor = '#FF0000';
isPasswordsMatch = false;
}
} else {
pw2.style.backgroundColor = '';
pw2.value = '';
isPasswordsMatch = false;
}
}
</script>
</body>
</html>
::::