checking and adding values for radio buttons

  • Eyesbryte
  • Beginner
  • Beginner
  • User avatar
  • Posts: 46
  • Loc: California

Post 3+ Months Ago

Hello All!

I'm looking for a little help with radio buttons in a form. Id like to accomplish two things.
1.) check all of the radio buttons and return an error message if one is unchecked directing the user to the specific radio button they'd forgotten

2.)add the value (an integer) of the radio buttons together and display the result

I have the code to accomplish either one or the other of the two things, but not both. Can you please help????

Thanks in advance!
-Brooke

Here's the code:
Code: [ Select ]
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
 if(el[i].type == "radio") {
  var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
  var itemchecked = false;
  for(var j = 0 ; j < radiogroup.length ; ++j) {
  if(radiogroup[j].checked) {
     itemchecked = true;
     break;
    }
  }
  if(!itemchecked) {
  alert("Please choose an answer for "+el[i].name+".");
  if(el[i].focus)
   el[i].focus();
    return false;
  }
 }
}

}
  1. function checkRadios() {
  2. var el = document.forms[0].elements;
  3. for(var i = 0 ; i < el.length ; ++i) {
  4.  if(el[i].type == "radio") {
  5.   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
  6.   var itemchecked = false;
  7.   for(var j = 0 ; j < radiogroup.length ; ++j) {
  8.   if(radiogroup[j].checked) {
  9.      itemchecked = true;
  10.      break;
  11.     }
  12.   }
  13.   if(!itemchecked) {
  14.   alert("Please choose an answer for "+el[i].name+".");
  15.   if(el[i].focus)
  16.    el[i].focus();
  17.     return false;
  18.   }
  19.  }
  20. }
  21. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Eyesbryte, Please remember to use either code or php tags when posting code, thanks :D (I fixed your original post)
  • Eyesbryte
  • Beginner
  • Beginner
  • User avatar
  • Posts: 46
  • Loc: California

Post 3+ Months Ago

Oops...sorry!
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Here's how I did it...

Code: [ Select ]
    var value = new Array();
    value[0] = "value of radio button 1";
    value[1] = "value of radio button 2";
    value[2] = "value of radio button 3";
    value[3] = "value of radio button 4";
    value[4] = "value of radio button 5";
    value[5] = "value of radio button 6";
    value[6] = "etc, etc, etc";

var x = "";
function calc(){
    var form = document.FormName;
    var y = "";
    var i = 0;
    for (i = 0; i < form.radiobuttonName.length; i++){ //identical names for all RB's
        if(form.radiobuttonName[i].checked){
            y = value[i];            
            x = x + y;
        }
    }
    validate();
}
  1.     var value = new Array();
  2.     value[0] = "value of radio button 1";
  3.     value[1] = "value of radio button 2";
  4.     value[2] = "value of radio button 3";
  5.     value[3] = "value of radio button 4";
  6.     value[4] = "value of radio button 5";
  7.     value[5] = "value of radio button 6";
  8.     value[6] = "etc, etc, etc";
  9. var x = "";
  10. function calc(){
  11.     var form = document.FormName;
  12.     var y = "";
  13.     var i = 0;
  14.     for (i = 0; i < form.radiobuttonName.length; i++){ //identical names for all RB's
  15.         if(form.radiobuttonName[i].checked){
  16.             y = value[i];            
  17.             x = x + y;
  18.         }
  19.     }
  20.     validate();
  21. }

x = the sum of the values of all checked radio buttons

I used this code to add the associated value (associated using the array) of radio buttons if they were checked by the user. This will accomplish the adding portion of your problem. Once this function has run it calls the validate function. In that function you can error trap. Aka...alert when any of the radio buttons aren't checked. If need be I can write you a lil code for that function. Just let me know by PM.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 57 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.