Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to give 'value' from Selected index in select list to "timeZone" attribute in JavaScript

What I have tried:

HTML
<!doctype html>
<html>

<head>
    <title>Javascript</title>
</head>

<body>
    <div id="container">
        <h1 id="time">Time now is: </h1>
    </div>
    <div id="container2">
        <select name="time_zone" id="time_zones">
            <option value="country11" disabled selected hidden>Select Country</option>
            <option value="Africa/Abidjan">Ivory Coast</option>
            <option value="Africa/Accra">Ghana</option>
        </select>
    </div>

    <script src="project 1.js"></script>
</body>

</html>

JavaScript
let new_date = new Date().toLocaleString("en-US", { timeZone: 'Asia/Qatar', timeStyle: "medium", hourCycle: "h12" });

time_zones.options[time_zones.selectedIndex].value

time_zones.addEventListener('change', () => {
    time_zones.options[time_zones.selectedIndex].value

})
Posted
Updated 27-Jul-21 4:56am

1 solution

You have the code there to support it, it's just not been put in the correct order. You need to build your date and update the content within the event listener code. For example:
JavaScript
time_zones.addEventListener('change', () => {
  const timezone = time_zones.options[time_zones.selectedIndex].value;
  const date = new Date().toLocaleString("en-US", {
    timeZone: timezone,
    timeStyle: 'medium',
    hourCycle: 'h12'
  });

  // do something with the date here
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900