Some issues with your code:
- You only had an event listener on the first input. You’ll need to add an event listener to the second input as well.
- The value on
keydown
won’t contain the same value as onkeyup
. You’ll need to dokeyup
to keep up with user input.
Working fiddle here.
document.getElementById("text1").addEventListener("keyup", testpassword2);
document.getElementById("text2").addEventListener("keyup", testpassword2);
function testpassword2() {
var text1 = document.getElementById("text1");
var text2 = document.getElementById("text2");
if (text1.value == text2.value)
text2.style.borderColor = "#2EFE2E";
else
text2.style.borderColor = "red";
}
<body>
<input type="text" id="text1" size="30">
<input type="text" id="text2" size="30">
</body>
1
solved Javascript compare two text fields