View Single Post
Old 12-14-2012, 06:50 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb Recommended modifications

Here I have incorporated your recommendations, if of interest to anyone else...
Code:
<!DOCTYPE HTML>
<html>
<head>
<title> CBox/RBtn/SBox Tests </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body>
<form id="myForm" action="" method="post" onsubmit="return false">
<table border="1">
 <tr>
  <td valign="top"> CBox<br>
   <input type="checkbox" name="cbox" id="cb0" value="cb-0"> 0<br>
   <input type="checkbox" name="cbox" id="cb1" value="cb-1"> 1<br>
   <input type="checkbox" name="cbox" id="cb2" value="cb-2"> 2<br>
   <input type="checkbox" name="cbox" id="cb3" value="cb-3"> 3<br>
   <input type="checkbox" name="cbox" id="cb4" value="cb-4"> 4
  </td>
  <td valign="top"> RBtn<br>
   <input type="radio" name="rbtn" value="rb-A"> A<br>
   <input type="radio" name="rbtn" value="rb-B"> B<br>
   <input type="radio" name="rbtn" value="rb-C"> C<br>
   <input type="radio" name="rbtn" value="rb-D"> D<br>
   <input type="radio" name="rbtn" value="rb-E"> E
  </td>
  <td valign="top"> Single CBox<br>
   <input type="checkbox" name="cboxAgree" id="cbAgree" value="cbAgree"> CBox Agree
  </td>
  <td valign="top"> Single RBtn<br>
   <input type="radio" name="rbtnAgree" value="rbAgree"> RBtn Agree<br>Kinda Silly
  </td>

  <td valign="top">  Single DD<br>Selection<br>
   <select id="sboxS" name="sboxS">
    <option value="">Choose One</option>
    <option value="ssb-0">0</option>
    <option value="ssb-1">1</option>
    <option value="ssb-2">2</option>
    <option value="ssb-3">3</option>
    <option value="ssb-4">4</option>
   </select>
  </td>
  <td valign="top"> Multiple DD<br>Selection<br>
   <select id="sboxM" name="sboxM" multiple>
    <option value="">Choose One Or More</option>
    <option value="msb-A">a</option>
    <option value="msb-B">b</option>
    <option value="mab-C">c</option>
    <option value="msb-D">d</option>
    <option value="msh-E">e</option>
   </select>
  </td>
 </tr>
</table> <p> <button onclick="statusDisplay()">Status Report</button>
</form>

<script type="text/javascript">

// Following from: http://www.codingforums.com/showthread.php?t=284053
function getGroupValue( group ) {
    if ( group == null ) { return false; } // OMIT this if you know there is at least one 
// handle the case of only one item in the "group"
    if ( group.length == null ) { return group.checked ? [ group.value ] : false; }
//  if ( group.length == null ) { return group.checked ? [ 0 ] : false; }  // to return position, instead of value

    var allchecked = [ ];
    for ( var g = 0; g < group.length; ++g ) {
         if ( group[g].checked ) { allchecked.push( group[g].value ); }
//       if ( group[g].checked ) { allchecked.push( g ); }  // to return position, instead of value, of selections
    }
    return allchecked.length > 0 ? allchecked : false; 
}

// Modified from above for Drop-Down Selections (single and multiple)
function getSelectValue( group ) {
    if ( group == null ) { return false; } // OMIT this if you know there is at least one 
// handle the case of no items in the "group"
    if ( group.options.length == null ) {
        return (group.selectedIndex != -1) ? [ group.options[group.selectedIndex].value ] : false;
        return (group.selectedIndex != -1) ? [ group.selectedIndex ] : false;  // to return position, instead of value
    }

    var allchecked = [ ];
    for ( var g = 0; g < group.options.length; ++g ) {
         if ( group.options[g].selected ) { allchecked.push( group.options[g].value ); }
//       if ( group.options[g].selected ) { allchecked.push( g ); }  // to return position, instead of value, of selections
    }
    return allchecked.length > 0 ? allchecked : false; 
}

function statusDisplay() {
  var frm = document.getElementById('myForm');
  var cb = getGroupValue(frm.cbox);
  var rb = getGroupValue(frm.rbtn);
  var cbA = getGroupValue(frm.cboxAgree);
  var rbA = getGroupValue(frm.rbtnAgree);
  var sbS = getSelectValue(frm.sboxS);
  var sbM = getSelectValue(frm.sboxM);
  var str = 'cbox: '+cb+'\n\nrbtn: '+rb+'\n\nsingle cbox: '+cbA+'\n\nsingle rbtn: '+rbA
          + '\n\nsingle select: '+sbS+'\n\nmultiple select: '+sbM;
  alert(str);
}
</script>

</body>
</html>
jmrker is offline   Reply With Quote