Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

radiobutton checked or not?

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I want to check if a radiobutton is checked or not. How do I check if it's not checked. I use this syntax;
if(document.mail.elements["radio"].checked=false)

It doesn't work, any ideas?

Thanks / Henrik
 
Radio button properties behave a little differently than checkbox properties. Since radio buttons can be layed out in a variety of different ways, before I can give you a solution for yours, I need to see your collection of buttons.

Can you send those over?

ToddWW
 
OK. One thing to remember is that the radio collection is treated a little differently than the checkboxes. While checkboxes are single elements, radio buttons can have many options (or buttons) within a single element. I always think of radio buttons to behave a little like select lists. Radio buttons and select lists share a common property and that is length and index number [n]. Length being the number of radio buttons in the collection and index number referring to the button number starting with zero.

I looked at your page and it appears that you simply want to make sure that A button is checked before submittal. To do that, you're going to need to loop through the buttons until you get to a button that is checked, then return true. If return true is never met, then you issue the alert, then return false. See example below

Below is a run-ready page that shows the validation. I looked at your site and used the same form / element / function names, so if you're happy with it, you should just be able to copy the function right into your page and it will work.

Code:
<html>
<head>

<script language=&quot;javascript&quot;>
<!--

function validatepoll() {
  for (var m = 0; m < document.poll.radio.length; m++) {
    if(document.poll.radio[m].checked == true) {
      return true
    }
  }
  window.alert(&quot;Choose first, then vote!&quot;)
  return false
}

// -->
</script>
</head>
<body>
<form name=&quot;poll&quot; onSubmit=&quot;return validatepoll();&quot;>
  <input type=&quot;radio&quot; name=&quot;radio&quot; value=&quot;r1&quot;>..guestbook<br>
  <input type=&quot;radio&quot; name=&quot;radio&quot; value=&quot;r2&quot;>..diaries</input><br>
  <input type=&quot;radio&quot; name=&quot;radio&quot; value=&quot;r3&quot;>profiles</input><br>
  <input type=&quot;submit&quot; value=&quot;Vote&quot;>
</form>
</body>
</html>

Hope this helps .. :)

ToddWW
 
thanks! I had to put a return false in the onsubmit as well but it works great now! thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top