Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help with radio button values

    Am trying to use radio buttons (yes/no) in a javascript.
    i need the answers to a series of questions to have a numeric value. for example: 'yes' is worth 2 points, 'no' is worth 0 points.
    after answering all the questions, a score (point total) is calculated.
    i'm able to make this work with drop down boxes, but radio buttons are a mystery.
    please consider me a novice with any reply.
    thanks for your help.

  • #2
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Something like:
    if(my_radio.checked=true){
    points=2
    }
    else{
    points=1
    }


    should work

    radio buttons (scroll down a little)

    Set and return values from radio buttons (example with editting possibility)
    Shawn

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here is something simply that might help you out some
    Code:
    <form name="radies">
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    </form>
    
    <script type="javascript">
    fm = docuemnt.radies;
    function checkR() {
    for(x=0;x<fm.length - 1;x++) {
    if(fm.elements[x].checked && fm.elements[x].value=="1")
    total+=2;
    }
    }
    </script>

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Typo

    Code:
    <form name="radies">
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    </form>
    
    <script type="javascript">
    fm = docuemnt.radies;
    function checkR() {
    for(x=0;x<fm.length - 1;x++) {
    if(fm.elements[x].checked && fm.elements[x].value=="1")
    total+=2;
    }
    }
    </script>
    docuemnt should be replaced by document
    Shawn

  • #5
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more help please

    Thanks for the replies.
    Sorry to bother you again.

    i've been playing with this for a week and have not been able to make it work for what i need. (basically i'm too new at this to understand what's happening).

    each question will have it's own 'yes' or 'no.' i applied group names to each set of boxes to separate them from each other.

    what i still don't get is....

    how do i make the total value appear in a text box?

    for example 7 'yes' answers yield 14 points (2 ea.)
    while 3 'no' answers yield 3 points (1 ea.)
    total '17.'

    thanks again

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Typo

    Off the top of my head:
    Code:
    <form name="radies">
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    
    yes <input type="radio" value="1">
    no <input type="radio" value="0">
    
    points <input type="text" value="0">
    </form>
    
    <script type="javascript">
    fm = document.radies;
    function checkR() {
    for(x=0;x<fm.length - 1;x++) {
    if(fm.elements[x].checked && fm.elements[x].value=="1")
    total+=2;
    fm.points==total
    }
    }
    </script>
    I think this should work...
    Shawn

  • #7
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    still at a loss

    i really appreciate your efforts and patience. but i still can't get this to happen (too dense).

    i named the groups so i could select a radio button.
    had to give them two names so the yes/no pairs would respond separately. but i still can't get it to total the values. here's what i'm trying to do...


    <p>Question1 <input type="radio" value="1" name="Q1">yes <input type="radio" value="0" name="Q1">no</p>
    <p>Question2 <input type="radio" value="1" name="Q2">yes <input type="radio" value="0" name="Q2">no</p>
    <p>Question3 <input type="radio" value="1" name="Q3">yes <input type="radio" value="0" name="Q3">no</p>
    <p><input type="text" name="score" size="5"></p>
    <p><input type="button" value="tally" name="B1"></p>

    when the 'tally' button is clicked, the values of Questions 1 - 3 should be added together and appear in the 'score' text box.

  • #8
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *Lightbulb clicks*

    You can only have one set of radio buttons per form (i.e. one question, multiple answers).

    For multiple questions with yes/no answers, you need to use checkboxes. Either one checkbox per question (on=yes, off=no) or have two boxes if there are 3-4 answers (one=yes, other=no, none=something else, both=something else else).

    Radio buttons and Checkboxes were designed for different things. For yes/no question, checkboxes are the way to go.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #9
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple radio groups in a single form

    Here's an example of multiple radio groups in a single form.

    You can try this by going to w3schools and paste the html into the panel on the left, then click 'Edit the text and click me'.

    http://www.w3schools.com/js/tryit.as...ame=tryjs_text

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    function showResult(itemNdx)
    {
      var form = document.forms[0];
      var result = document.getElementById('result');
      var elem = form.elements[itemNdx];
      var itemName = elem.name;
      result.innerHTML = 'Item name: ' + elem.name + ', id: ' + elem.id;
    }
    
    </script>
    
    <form name="input" action="">
    
    <p>Are you a girl or a guy?</p>
    <input type="radio" name="sex" id="male" onclick=showResult(0) />
    <label for="male">Male</label>
    <br />
    <input type="radio" name="sex" id="female" onclick=showResult(1) />
    <label for="female">Female</label>
    
    <p>Are you a bird or a mammal?</p>
    <input type="radio" name="kind" id="bird" onclick=showResult(2) />
    <label for="bird">Bird</label>
    <br />
    <input type="radio" name="kind" id="mammal" onclick=showResult(3) />
    <label for="mammal">Mammal</label>
    
    </form>
    
    <p>Result:</p>
    <div id="result"></div>
    
    </body>
    </html>


  •  

    Posting Permissions

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