I am working on a script and made it to function; however, I can't figure out how to make the @giobianchi's code work when the user hits "enter" on the keyboard. I assume we'd need to add another event listener with "keypress" or similar. I'd need the messages to show up on either enter or click.
The code is essentially from this thread. Thanks to the helpers there! I'm just struggling to find a solution that would work with this setup. Unless there's a better way to achieve the same thing?
Would greatly appreciate some guidance!
<!DOCTYPE html>
<html>
<body>
<input type="text" id="zipCode" placeholder="Enter ZIP code" />
<button id="checkButton">Check</button>
<div id="msg"></div>
<script>
var button = document.getElementById("checkButton");
function checkIfAvailable(zip) {
let zones = ["60004", "60005", "60006"]
return (zones.indexOf(zip) >= 0);
}
button.addEventListener("click", function validateZip() {
let zip = document.getElementById("zipCode").value;
let msg = "";
if (checkIfAvailable(zip)) {
msg = "Service is available your area!";
} else {
msg = "Sorry, we do not service your area yet.";
}
document.getElementById("msg").innerHTML = msg;
});
</script>
</body>
</html>
I tried adding a second event listener with "keyup" to no avail.
<html>
<body>
<input type="text" id="zipCode" placeholder="Enter ZIP code" />
<button id="checkButton">Check</button>
<div id="msg"></div>
<script>
var button = document.getElementById("checkButton");
function checkIfAvailable(zip) {
let zones = ["60004", "60005", "60006"]
return (zones.indexOf(zip) >= 0);
}
button.addEventListener("click", function validateZip() {
let zip = document.getElementById("zipCode").value;
let msg = "";
if (checkIfAvailable(zip)) {
msg = "Service is available your area!";
} else {
msg = "Sorry, we do not service your area yet.";
}
document.getElementById("msg").innerHTML = msg;
});
button.addEventListener("keyup", function validateZip() {
let zip = document.getElementById("zipCode").value;
let msg = "";
if (checkIfAvailable(zip)) {
msg = "Service is available your area!";
} else {
msg = "Sorry, we do not service your area yet.";
}
document.getElementById("msg").innerHTML = msg;
});
</script>
</body>
</html>