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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick handler

Status
Not open for further replies.

FederalProgrammer

Programmer
Jul 2, 2003
318
CA
I need to display at most 3 radio buttons depending on the logged-in user (silver members will get 1 radio button, while for a gold member there will be 2 radio buttons displayed and for plat members, there will be 3 radio buttons);

Radio buttons are displayed correctly; My problem is with the onclick function (a javascript function).
My gold and plat members have no problem. But silver members (w/ a single radio button) fail:

Code:
for ($i = 0;$i < count($rooms); $i++)
{
$result = mysql_query("SELECT * FROM rooms WHERE id={$rooms[$i]}");
$row = mysql_fetch_object($result);
					
$roomNum = $rooms[$i];
echo "<input type='radio' name='rdiChat' value='$roomNum' onclick = HandleChatroomSelected();>$row->name</a>";
}

following javascript fails (for rdiChat.length = 1)!!
Code:
var selectedChat = 'Donot know yet';
for (i = 0; i < document.chat.rdiChat.length; i++)
{
    //this line doesn't get executed for rdiChat.length = 1:
    document.write(document.chat.rdiChat.length);

    if (document.chat.rdiChat[i].checked)
    {
	selectedChat = document.chat.rdiChat[i].value;
    }
}


---------------
 
thanx for the tip ;)
I thought may be it has anything to do w/ the way I'm defining my onclick event handler though?

I mean, I am not sure where the code is going wrong. I just know that rdiChat is undefined in the script when the user has access to a single radio button :(
So I'm not sure where this is originated...

once again, thanx for the tip though :)

cheers!


---------------
 
I think it is because if there is only one radiobutton it isn't an array.

Try something like this in the javascript:

Code:
var selectedChat = 'Donot know yet';

if (rdiChat[0]) //checks if it is an array
{
   for (i = 0; i < document.chat.rdiChat.length; i++)
   {
   
    document.write(document.chat.rdiChat.length);

    if (document.chat.rdiChat[i].checked)
    {
    selectedChat = document.chat.rdiChat[i].value;
    } 
}
else // This is executed if there is only one radiobutton
{
    if (document.chat.rdiChat.checked)
    {
    selectedChat = document.chat.rdiChat.value;
    }
}

I didn't test this but I think it might fix your problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top