?
Solved

check radiobutton

Posted on 2001-07-18
3
Medium Priority
?
287 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

839 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