I’ve been having so much trouble getting my JavaScript to check that the emails in my HTML form match and display results. I’ve tried doing it like the zyBook for my class shows, but I just get nothing.
I tried to have the function compare the input of #email and #cemail and change the background color of the field to green when they matched and to red when they didn’t match. I tried to create an event listener to run the function whenever the input for #cemail changed. I’m using Visual Studio, and one of the main problems that I notice is that when I add ".value" or ".style" or anything like it, they don’t get suggested as a method of the objects in the function. It seems like I’m referencing something that doesn’t exist, but the IDs match, so I don’t know why it doesn’t register.
Here is the relevant code:
<form id="userInfo">
<label for="fname">First Name:</label>
<input type="text" id="fname" name="fname" placeholder="John"><br>
<label for="lname">Last Name:</label>,
<input type="text" id="lname" name="lname" placeholder="Doe"><br>
<label for="email">Email:</label>
<input type="text" id="email" name="email" placeholder="abc123@gmail
.com"><br>
<label for="cemail">Confrim Email:</label>
<input type="text" id="cemail" name="cemail"
placeholder="[email protected]"><br>
<label for="question">Write Question:</label>
<textarea id="question" name="question" size="50"
placeholder="My question is..."></textarea><br>
<input type="submit" value="Submit">
</form>
<script>
let cemail = document.querySelector("#cemail").value;
let formWidget = cemail.addEventListener("change", checkEmail)
function checkEmail(){
let email = document.querySelector("#email").value;
if (email.trim() === cemail.trim()) {
email.style.backgroundColor = "green";
cEmail.style.backgroundColor = "green";
return true;
} else {
email.style.backgroundColor = "red";
cEmail.style.backgroundColor = "red"; }
}
</script>
3
Answers
Due to my low reputation, I can’t comment and ask follow up questions, but I have some things to say that might help.
Log the cemail variable and see if it is null. VS Code should be giving you values for the elements, so if it isn’t, maybe cemail null.
Instead of document.querySelector(), try document.getElementById(). Sometimes you need to run a different function that does almost the same thing.
I highly bet you know querySelector more then I do, but here is the Mozilla link for querySelector in case there is something we both have overlooked.
Hope this helps.
the querySelector returns
meaning you get all sorts of attributes and methods to interact with the feild
You had to keep an reference to the Element and access its value each time you wanted to compare with something. and using the same reference, you’ll be able to update its attributs (like bg color)
once you remove the
.value
VS Code should help you with intellisensehere’s the working code :
I think it is better to use e.target.value for the confirmation email input like this you can instantly show to user if it is matching or not while typing.