Solved

2 radio buttons and simple function

Posted on 2001-09-10
7
203 Views
Last Modified: 2008-03-03
<script language="javascript">
function enabled(chosen)
{
alert(chosen);
//This function takes the option that is chosen and disables the other two
     if (chosen == "meter")
          {
          document.TheForm.region.selectedIndex = 0;
          document.TheForm.district.selectedIndex = 0;
          document.TheForm.usage.checked = false;
          }
     else if(chosen == "district")
          {
          document.TheForm.meter.selectedIndex = 0;
          document.TheForm.region.selectedIndex = 0;
          }
     else if(chosen == "region")
          {
          document.TheForm.meter.selectedIndex = 0;
          document.TheForm.district.selectedIndex = 0;
          }
     else
          {
          document.TheForm.meter.selectedIndex = 0;
          }
}
</script>



<input type="radio" name="usage" value="van" onClick="enabled(document.TheForm.usage.value);">

<input type="radio" name="usage" value="total" onClick="enabled(document.TheForm.usage.value);">

Whenever the radio buttons are clicked on, my alert statment says chosen is undefined.  I am wanting to set both radio buttons to unchecked if the meter drop box is selected.  Any thoughts?  

-m
0
Comment
Question by:mudface061200
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:estrauss
ID: 6471398
Radio buttons are a pain! They have no value just because they have been click (at least as far as JavaScript is concerned. I have written the function below to get the value of the checked radio button.


function getselectedbutton(buttongroup)
     {
      for (var i=0; i <buttongroup.length;i++)
          {
          if (buttongroup[i].checked)
               {
               return buttongroup[i].value;
               break;
               }
          }
     alert("you must select a value from the radio buttons");
     return(false);
     }


So, you should be able to do the following:
Add this function to your script
Change the onclick to enable(document.TheForm.usage)
Add the following to your function
newvariablename= getselectedbutton(chosen)


I haven't checked the rest of it, so there may some other minor changes, but that should basically do it.
Ethan
0
 
LVL 1

Author Comment

by:mudface061200
ID: 6471416
Wow, there isn't a cleaner way to clear 2 radio buttons if a drop down menu is selected?

-m
0
 
LVL 1

Accepted Solution

by:
estrauss earned 50 total points
ID: 6471441
None that I know of. You could always pass the value directly somehow. Something like:

onClick="enabled('radio1chosen');"

and then check for that value. Since you only have two radio buttons that should be fine.
Ethan


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:mudface061200
ID: 6471501
Thanks!  I don't know why I didn't think of that earlier.

-m
0
 
LVL 2

Expert Comment

by:psogaa
ID: 6471518
You need to use the object hierachy to pass the value of the selected radio button, like this:

<form name=TheForm>
<input type="radio" name="usage" value="van" onClick="enabled(this.value);">
<input type="radio" name="usage" value="total" onClick="enabled(this.value);">
</form>

I'm not sure what it is you exactly are trying to do.
If you you post the whole code including the drop down menus and explain it a little better, i'll mix up a working sollution for you :)
0
 
LVL 1

Author Comment

by:mudface061200
ID: 6471659
psogaa,

Once inside my function, how would I reference those radio buttons?  I.E. How can I tell them to be unselected?
0
 
LVL 1

Author Comment

by:mudface061200
ID: 6472419
Nevermind.  I now understand radio buttons are handled as arrays..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now