Solved

check radiobutton

Posted on 2001-07-18
3
263 Views
Last Modified: 2012-05-04
I would like to check if one of my radiobuttons is checked.
By default, NONE of the radio-buttons must be pre-checked
Problem: with my currently script, it checkes which value is checked, BUT if none of the radio buttons is checked, it gives automatically the value of the first radio button.

<radio name=a1 value=1>
<radio name=a1 value=2>
<radio name=a1 value=3>
<radio name=a1 value=4>
<radio name=a1 value=5>


function getSelectedButton(buttonGroup)
  {
  for (var i=0;i<buttonGroup.length;i++)
    {
    if (buttonGroup[i].checked)
     {
     return i
        }
    }
  return 0
  }

var i=getSelectedButton(document.form.a1)
alert("You chose "+ form.a1[i].value + ".")
0
Comment
Question by:moswa
3 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 50 total points
Comment Utility
function getSelectedButton(buttonGroup)
{
   for (var i=0;i<buttonGroup.length;i++)
   {
      if (buttonGroup[i].checked)
      {
         return i
      }
   }
   return -1;
}

var i=getSelectedButton(document.form.a1)
if(i<0) alert("No button checked")
else alert("You chose "+ form.a1[i].value + ".")
0
 
LVL 1

Author Comment

by:moswa
Comment Utility
thanx,

moswa
0
 
LVL 30

Expert Comment

by:third
Comment Utility
your function returns 0 :

function getSelectedButton(buttonGroup){
 for (var i=0;i<buttonGroup.length;i++){
   if (buttonGroup[i].checked) {
    return i
   }
 }

// this statement makes it zero
 return 0
}

u need to remove that part.... :^) third

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now