Click to See Complete Forum and Search --> : Radio Button Disaster


Gecks1982
May 18th, 2005, 05:24 AM
Hi i am new at this and am trying to assign a value to a radio within a function.
I think I have the If statement right but I read that I will need a FOR loop. I need help to write this one.

here is the If statment within the function

if (frmHotel.RmStyle.value == 0){varRoomStyle = "55"};
else{if(frmHotel.RmStyle.value == 1) {varRoomStyle = "65"}
else {varRoomStyle = "80"};};

Then in my code I have enteed the radio buttons as such

<INPUT TYPE=Radio NAME= "RmStyle" value = 0>
<INPUT TYPE=Radio NAME= "RmStyle" value = 1>
<INPUT TYPE=Radio NAME= "RmStyle" value = 2>

I know its probably something really simple.

Regards

CLUELESS!!!

Dr. Script
May 18th, 2005, 08:45 AM
if(frmHotel.RmStyle[0].checked) varRoomStyle = 55;
else if(frmHotel.RmStyle[1].checked) varRoomStyle = 65;
else(frmHotel.RmStyle[2].checked) varRoomStyle = 80;

Vanny
May 23rd, 2005, 10:30 PM
A good old favourite of mine,


function getRadioValue(radioName)
{
var collection = null;
collection = radioName
for (i=0 ; i < collection.length;i++)
{
if (collection[i].checked)
return(collection[i].value);
}
}


Allows you to add more options anywhere in the sequence with out have to update other javascript code. Just use it like

if (getRadioValue(frmHotel.RmStyle) == 0){varRoomStyle = "55"};

John S
May 24th, 2005, 08:33 AM
For more than an two conditions it is better to use a switch statement.

function fnSwitch(arg)
{
var varRoomStyle;

switch (arg)
{
case "0":
return "55"; // (or you could use ) varRoomStyle = "55"; break;
case "1":
return "65"; // (or you could use ) varRoomStyle = "65"; break;
default:
return "80";// (or you could use ) varRoomStyle = "80"; break;
}

// return varRoomStyle; // you need this if you choose other (better?) method
}

// called like so
alert(fnSwitch("1"));

Dr. Script
May 24th, 2005, 02:52 PM
... never use a switch statement ... they aren't coded well enough in JavaScript to be stable, imo. The if decision structure is more reliable.