Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!