Solved

check radiobutton

Posted on 2001-07-18
3
267 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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. …
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…

821 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