Click to See Complete Forum and Search --> : menu passing value to script


learningJavaS
10-06-2005, 03:15 AM
Hi,

I am trying to write a script that changes the time based off of a pull down menu option for a new time zone. Nothing is happening when I select a city.

Can you spot something I am doing wrong?

Thank you for your help.

Here's the form in html...

<div class="select">
<select name="city" id="city" size="1" onchange="myCity(this)">
<option value="" selected>Your time</option>
<option value="0">London</option>
<option value="1">Stockholm</option>
<option value="2">Helsinki</option>
<option value="8">Hong Kong</option>
<option value="9">Tokyo</option>
<option value="10">Sydney</option>
<option value="-8">San Francisco</option>
<option value="-6">Chicago</option>
<option value="-5">New York</option>
</select>
</div>

Here is the javaScript...

var zone = 0;
var localTime = true;

function myCity() {
zone = document.getElementById(city).value;
localTime = (document.getElementById(city).selected) ? true : false;
}

function tid() {

var flip = new Date();
var hours = (localTime) ? flip.getHours() : (internationalTime + zone);
var minutes = flip.getMinutes();
var internationalTime = flip.getTimezoneOffset()/60;

dayTime = "AM";
if ((hours >= 12) && (minutes >= 1) || (hours >= 13)) {
dayTime = "PM";
hours = hours-12;
}

if (hours == 0)
hours = 12;
displayFlip(hours,minutes,dayTime);
nightWatch(dayTime);
setTimeout("tid()", 1000);
}

Kor
10-06-2005, 03:19 AM
function myCity(obj) {
zone = obj.value;
localTime = obj.options[obj.selectedIndex].text//if this is the value you need
alert(zone);
}

learningJavaS
10-06-2005, 03:34 AM
Hi,

I just tried that and it didn't work, any other sugesstions?

How do I check for the value selected in the drop down?

Thank you for the post.

Kor
10-06-2005, 03:40 AM
should work.

the value of the select element is always the value of the selected option

function myCity(obj) {
zone = obj.value;//returns the value of the selected option
localTime = obj.options[obj.selectedIndex].text//returns the text of the selcetd option
}

Which are the values you want to get?

learningJavaS
10-06-2005, 03:48 AM
Hi Kor,

Thank you for the help.

I need to get both values. Please see my "hours" varible.

var hours = (localTime) ? flip.getHours() :(internationalTime + zone);

I need to get the values so I can figure out what the hour is. So for the "localTime" varible I need it to give me a true or flase answer?? I think... I don't know now, I am still learning JavaScript.

Thank you for your posts, I appreciate it.

Kor
10-06-2005, 03:57 AM
aha... you need to know if a zone is selected... And you need numbers (keep in mind that the elements' HTMl attributes have strings as values, not numbers)?

try this:

function myCity(obj) {
zone = Number(obj.value);
localTime = (obj.selectedIndex>0)?true:false;
}

Which the final purpose of the code?

learningJavaS
10-06-2005, 06:11 AM
Hi Kor, Thank you for your help. I tried it out and add something else and it looks like it is working now.

Now I just need to add the menu to adjust for daylight savings time. How can I check the value of a different menu?

I now have two menus, one for picking the city and the other for picking daylight savings or not.

If daylight savings is picked it adds 1 to GMT value.

How can I check from the daylights savings menu if a city has been picked so I can change the daylight savings value?

I hope that makes sence.

Thank you for your time and help. Best regards.

var zone = 0;
var localTime = true;
var dst = 0;

function myCity(obj) {
zone = Number(obj.value)
localTime = (obj.options[0].selected) ? true : false;

if ((obj.options[obj.selectedIndex].text) == "Tokyo") {
dst = 0;

}
}

function dstSelect(obj) {
dst = (obj.options[2].selected) ? 1 : 0;
}