Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Set value of hidden input based on ZIP code entry

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");
});  
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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" );
  }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda