Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
Show div if select option item is selected. Problem with reloading

Hi I have HTML bootstrap code:

$(document).ready(function() {
  $("#us-state").find('select').change(function() {
    var $val = $(this).val();

    if ($val === 'US') {
      $('.us-state-select').show();
    } else {
      $('.us-state-select').hide();
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div class="col-sm mb-5  us-state-select">
  <label>USA STATE</label>
  <select name="order_from_address_state" class="form-select">
    <option>Choose country</option>
    <option value="AL" name="<AL>" selected>Alabama</option>
    <option value="AK" name="AK">Arkansas</option>
  </select>
</div>


<div class="col-sm mb-5" id="us-state"><label>Send from country</label>
  <select name="order_from_address_country" class="form-select">
    <option>Send from</option>
    <option value="US" name="US">USA</option>
    <option value="CA" name="CA" selected>Canada</option>
  </select>
</div>

(Also available on JSFiddle)

My problem is simple. I am using foreach to show all countries and statesm but... check the jsfiddle. if country is selected different to the US and its load with this different country, it shows div us-state-select and if u click on different countries, it dissapears and works as expected. But I dont want to show div with states in the case that page is loaded and its selected different country than US. How to do that? its obvious on fiddle, I selected canada and on page load there is div with states and when click on different value for example: Send from at country box, states dissapear and it works as expected then. Please, can you give me some advice? I am newbie at jquery, thx.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change your JS to be like this: I am basically checking this logic once the page is ready and I also added some refactor in your code to help readability

$(document).ready(function() {
const checkValues = () => {
        var value = $("#us-state").find('select').val();
    console.log(value)
    if(value === 'US'){
        $('.us-state-select').show();
    }else{
        $('.us-state-select').hide();
    }
}

        $("#us-state").ready(checkValues);
    $("#us-state").find('select').change(checkValues);
});

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda