I want if the local time is in europe that the option europe changes place under the heading "Europe"
This is what I came with:
var userCountry = ""; // Store user's country here (e.g., obtained through geolocation)
var europeanCountries = ["Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden"];
if (europeanCountries.includes(userCountry)) {
var localOption = document.querySelector("#timezone [value=local]");
var europeOptgroup = document.getElementById("europe-optgroup");
europeOptgroup.appendChild(localOption);
}
<select id="timezone" onkeyup="filterFunction(this,event)" onchange="changeTimezone()">
<option value="local" selected>Local Time</option>
<optgroup label="Europe" id="europe-optgroup">
<option value="GMT">London</option>
<option value="Europe/Amsterdam">Amsterdam</option>
<option value="Europe/Athens">Greece</option>
<option value="Europe/Moscow">Moscow</option>
</optgroup>
<optgroup label="USA">
<option value="America/New_York">New York</option>
<option value="America/Los_Angeles">Las Vegas</option>
</optgroup>
</select>
2
Answers
You could pass the
<select>
element to a function that does the shuffling for you.Not sure what your exactly looking for… but if your looking for getting location and setting option is selected.
var country = Intl.DateTimeFormat().resolvedOptions().timeZone;
is one option. i.e the output could beAmerica/Toronto
orEurope/Athens
where you are located it will be different for testing.Then you could check if that has which you have requested
Europe
then you can set the value of the options based on if it has Europe in it. Or else it will default the option of local time.Then you can use the select Value property change the option based on where there are located.
var localOption = document.querySelector("#timezone");
localOption.value = country;
https://www.w3schools.com/jsref/prop_select_value.asp
***just of note this will not work unless your are actually in Europe or you Manually set your location. ***
Here is an example of what you might be looking for
https://jsfiddle.net/5nvkby3q/