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

152
Views
Select option stays after leaving page

I have a select menu that I am attempting to implement on my page for a user to select a country.

My current options are Canada, United States and Global. I am attempting to add the selected attribute on Canada when the page loads so Canada is by default the country selected. This is working as expected but I noticed if I selected another country for example "United States" visit another page on the website and go back via the back arrow the country I selected before redirecting in this case "United States" is still the selected country and not Canada.

If I inspect I see that Canada has the selected attribute, but United States is still shown as the one selected.

I've attempted to add

  document.getElementById("country-select").selectedIndex = -1;

Which I belive should remove the selected attribute but this is not working.

How can I reset my select drop down menu and display Canada by default even after returning back to the page.

I don't know if I will be able to re-create my issue on a code snippet but

Here is my code snippet:

    document.getElementById("country-select").selectedIndex = -1;


let selectedCountry = document.getElementById(124);
     // set Canada as default selected country
    selectedCountry.setAttribute('selected', 'true')
v    <select name="country" id="country-select">
          <option id="0" value="0">Global</option>
     <option id="1" value="1">United States</option>
      <option id="124" value="124">Canada</option>
  </select>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can set the selected option when the user navigates away from the page.

This is accomplished by listening for the beforeunload event:

window.onbeforeunload = function(){
  document.getElementById("country-select").value = "124";
}
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!