Jump to content

linking radio buttons and if statements


unistake

Recommended Posts

ok, im starting this again.

 

Hi all!

 

I am trying to link up 3 radio buttons and JS so that when selected, JS executes the correct if statements.

The code I have attempted to make so far is below, however I can not seem to get any of the JS below to work. Could someone show me where I am going wrong?

Also preferably instead of using the form button to execute the JS, I would like it if when the correct radio button is clicked, that the JS would run from that command

 

Thanks,

Tim

 

 

<head>
<script type="text/javascript">
function calc ()
{
var radiochoice;


for(i=0;i<document.calcform.typeofbet.length;i++)
{
	if(document.calcform.typeofbet[i].checked)
		radiochoice = document.calcform.typeofbet[i].value;
}


if (radiochoice=="qualifier")
{
function qualifier ()
}
if (radiochoice=="sr")
{
function sr ()
}
if (radiochoice=="snr")
{
function snr ()
}


function qualifier ()
{
	document.write("this is function qualifier!");
}
function sr()
{
	document.write("this is function SR!");
}
function snr()
{
	document.write("this is function SNR!");
}
}
</script>
</head>
<body>
<form name="calcform">
  <p>
    <input type="radio" name="typeofbet" value="qualifier" checked>
  radio1 
  <input type="radio" name="typeofbet" value="freesr">
  radio2
  <input type="radio" name="typeofbet" value="freesnr">
  radio3<br>
  <input type="submit" value="Click To Calculate" onClick="calc();">
</p>
</form>
/////////// I want to show the text from each function here............ ////////////////////////////////
</body>

 

Link to comment
Share on other sites

It seems that you are trying to set the functions twice; once when you try to call to them and once when you actually set them.

 

try changing:

  if (radiochoice=="qualifier")
   {
   function qualifier ()
   }
   if (radiochoice=="sr")
   {
   function sr ()
   }
   if (radiochoice=="snr")
   {
   function snr ()
   }

 

to:

  if (radiochoice=="qualifier")
   {
   qualifier();
   }
   if (radiochoice=="freesr")
   {
   sr();
   }
   if (radiochoice=="freesnr")
   {
   snr();
   }

 

edit: i was looking at the if statements and the values don't match up either.

Link to comment
Share on other sites

Thanks BillyBob, but it still does not seem to work.

Also should the function calc() have a close bracket } just before the function qualifier?

The code I have is now...

<head>
<script type="text/javascript">
function calc ()
{
var radiochoice;


for(i=0;i<document.calcform.typeofbet.length;i++)
{
	if(document.calcform.typeofbet[i].checked)
		radiochoice = document.calcform.typeofbet[i].value;
}


if (radiochoice=="qualifier")
   {
   qualifier();
   }
   if (radiochoice=="freesr")
   {
   sr();
   }
   if (radiochoice=="freesnr")
   {
   snr();
   }


function qualifier ()
{
	document.write("this is function qualifier!");
}
function sr()
{
	document.write("this is function SR!");
}
function snr()
{
	document.write("this is function SNR!");
}
}
</script>
</head>
<body>
<form name="calcform">
  <p>
    <input type="radio" name="typeofbet" value="qualifier" checked>
  radio1 
  <input type="radio" name="typeofbet" value="freesr">
  radio2
  <input type="radio" name="typeofbet" value="freesnr">
  radio3<br>
  <input type="submit" value="Click To Calculate" onClick="calc();">
</p>

</form>
</body>

 

What values dont match Billybob?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.