Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Buttons and Time

Status
Not open for further replies.

RickLiebespach

Programmer
Sep 29, 2003
85
0
0
US
I have two questions that for me are related....
One is how to properly get the radiobutton selected and the other is how to increment a time value.

I have a RadioButton with the values of:
5 minutes
10 minutes
15 minutes
30 minutes
1 hour

I want the user to select a radiobutton and then I'll add that amount of time to the current time and store that in a variable.
What I'm getting is
When the user clicks "5 minutes" (the 1st radio button)
my GetrbSnoozeValue function is returning 60 (what I thought was what I'd get from the 5th radio button)
which my SetSnoozeAlarm function is concatenating to the current date and time returned by Date()
so I get
7/28/2004 11:05:2360

what I want/need is
7/28/2004 11:10:23

assuming the current date and time is 7/28/2004 11:05:23

My code looks like this:

function SetSnoozeAlarm()
{
var myAlarmTime;
var myOffSet;
var myAlarmMessage;

myOffSet = GetrbSnoozeValue();

myAlarmTime = Date() + myOffSet;

myAlarmMessage = GettxtCurrentReminderMessage();

SetMyAlarm(myAlarmTime, myAlarmMessage);
}

function GetrbSnoozeValue()
{
var myOffSetAmount = 0;
var oSnooze = document.getElementById("rbSnooze")

if (oSnooze.selectedIndex = 5) // 1 hour
{
myOffSetAmount = 60;
}
else if (oSnooze.selectedIndex = 4) // 30 minutes
{
myOffSetAmount = 30;
}
else if (oSnooze.selectedIndex = 3) // 15 minutes
{
myOffSetAmount = 15;
}
else if (oSnooze.selectedIndex = 2) // 10 minutes
{
myOffSetAmount = 10;
}
else if (oSnooze.selectedIndex = 1) // 5 minutes
{
myOffSetAmount = 5;
}
else // no radio button selected
{
myOffSetAmount = 0;
}

return myOffSetAmount
}


Rick Liebespach
 
Conditional tests should contain double equals signs.

if (oSnooze.selectedIndex == 5) {

}

etc.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
1. First thing name your radio buttons with the name parameter, not id. Only use the id parameter when your html element will have a unique name. Since you want to name all the radio buttons the same thing to group them, you cannot give them unique names via id, just use name instead.

2. Second, when you group radio buttons by giving them the same name, it creates an array of radio buttons, so there is no need for using selectedIndex (not sure that even works with radio buttons.)

3. When you call your onclick function on the radio buttons, pass the value of the radio buttons so that you don't have to loop thru the buttons to see what's checked.

4. Use the getTime method for date objects to increment the value.

Here's an example:
Code:
<script language=JavaScript>

function adjustTime(minuteValue) {
    var dateField = document.forms['blahForm'].elements['dateField']
    var dateValue = dateField.value
    var millisecondValue = minuteValue * 60 * 1000;
    if (dateValue == "") {return false}
    dateValue = new Date(Date.parse(dateValue));
    dateValue.setTime(dateValue.getTime() + millisecondValue);
    dateField.value = dateValue;
}

</script>
<body>
<form name=blahForm>
<input type=radio name=radioTime onclick='adjustTime(5)'>5 minutes<br>
<input type=radio name=radioTime onclick='adjustTime(10)'>10 minutes<br>
<input type=radio name=radioTime onclick='adjustTime(15)'>15 minutes<br>
<input type=radio name=radioTime onclick='adjustTime(30)'>30 minutes<br>
<input type=radio name=radioTime onclick='adjustTime(60)'>60 minutes<br>
<input type=text name=dateField>Type Date Here before selecting Radio
</form>
</body>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top