I am attempting to set arrays for specific regions and then compare them to the zip code entered to set the value of a hidden field (to name the region). Everything I enter sets the "Not Found". I'm stumped, any and all help would be greatly appreciated.
HTML:
<input id="zip" name="ZIPCODE" type="text" />
<input id="REGION" name="REGION" type="hidden" />
SCRIPT:
var eastZips = [19144, 19103, 19104];
var westZips = [90210, 90211, 90212];
$("#zip").keyup(function() {
if ($(this).val() == eastZips) {
$("#REGION").val("East");
} else if ($(this).val() == westZips) {
$("#REGION").val("West");
} else
$("#REGION").val("Not Found");
});
The value of the input will never match eastZips or westZips because you are comparing things that are not the same "type". With your stored variables being arrays and the input value being a string.
There are a few steps to take here to clear this up.
Although what you are inputing is a set of digits JS will read the field value as string, so you must make sure that you are processing that value to compare correctly to the integers you have stored. This is achievable by putting the field value through a parseInt
The next thing you want is to check if the input value is part of either array, this is achievable by using includes.
var eastZips = [19144, 19103, 19104];
var westZips = [90210, 90211, 90212];
$("#zip").keyup(function() {
// Store input value for easier access, and wrap
// it in a parseInt to ensure you are storing
// an integer for comparison.
var zip_value = parseInt( $(this).val() );
// Check if either array stores the input value
if ( eastZips.includes( zip_value ) ) {
$("#REGION").val( "East" );
} else if ( westZips.includes( zip_value ) ) {
$("#REGION").val( "West" );
} else {
$("#REGION").val( "Not found" );
}
});