www.webdeveloper.com
Results 1 to 2 of 2

Thread: Checking the value of checkbox

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    Checking the value of checkbox

    Hi

    I have written the following javascript: http://ectpro.co/calc/rate.html

    What I need to is if the user places a checkmark in either the
    Starter Pack
    Extended Pack
    Development Pack or
    Paymate Pack

    then the quantity of the checked item will be set to "1" and the rest will be set to "0".

    Here is a zipped copy of my code:
    http://ectpro.co/calc2.zip

    Thanks for any help

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    Your 'rate.html' link is broken.

    Your 'calc2.zip' file does not contain any of the checkmark elements you mention.

    Finally, the requirements you specify are better served with a radio button rather than a checkbox.
    Your value for a particular element selected by the user can be determined by looking for the only TRUE
    item checked in the radio button named group.

    Consider this...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <fieldset style="width:10em">
     <legend> Available Packs </legend>
     <input type="radio" name="packs" value="Starter Pack"> Starter Pack <br>
     <input type="radio" name="packs" value="Extended Pack"> Extended Pack <br>
     <input type="radio" name="packs" value="Development Pack"> Development Pack <br>
     <input type="radio" name="packs" value="Paymate Pack"> Paymate Pack
    </fieldset>
    <button onclick="showPack()">Show selection</button>
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?296007-Checking-the-value-of-checkbox
    
    function getRBtnName(GrpName) {
      var sel = document.getElementsByName(GrpName);
      var fnd = -1;
      var str = '';
      for (var i=0; i<sel.length; i++) {
        if (sel[i].checked) { str = sel[i].value;  fnd = i; }
      }
    //  return fnd;   // return option index of selection
    // comment out next line if option index used in line above  
      return str;
    } 
    
    function showPack() {
      var pick = getRBtnName('packs');
      if (pick == '') { alert('No selection chosen'); }
                 else { alert('User choice: '+pick); }
    }
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 06-05-2014 at 09:32 PM. Reason: Added example

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles