Solved

check radiobutton

Posted on 2001-07-18
3
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 50 total points
ID: 6293168
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
ID: 6293174
thanx,

moswa
0
 
LVL 30

Expert Comment

by:third
ID: 6293175
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 Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

707 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