Results 1 to 7 of 7

Thread: JavaScript Calculation Help?

  1. #1
    Join Date
    Jan 2007
    Location
    Washington DC
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScript Calculation Help?

    New to programming here was wounding if you can do calculation with JavaScript. Trying to build a form I have about 4 radio buttons list with a “yes” and “no” labels and each “Yes” has a value of 10 and “No” with a value of 0. Is there a way to total up the “Yes” values into a single field?

    Thanks in Advance
    Jonsey


    HTML Code:
    <input id="Radio1" type="radio" name="rblA" value="10"/>
    	Yes&nbsp;
    	<input id="Radio2" type="radio" name="rblA" value="0"/>
    	No<br />
    	<br />
    	<input id="Radio3" type="radio" name="rblB" value="10"/>
    	Yes&nbsp;
    	<input id="Radio4" type="radio" name="rblB" value="0"/>
    	No<br />
    	<br />
    	<input id="Radio5" type="radio" name="rblC" value="10"/>
    	Yes&nbsp;
    	<input id="Radio6" type="radio" name="rblC" value="0"/>
    	No<br />
    	<br />
    	<input id="Radio7" type="radio" name="rblD" value="10"/>
    	Yes&nbsp;
    	<input id="Radio8" type="radio" name="rblD" value="0"/>
    	No<br />
    	<br />
    	<br />

  2. #2
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Here is the solution...

    Please try this...Hope this is what you wanted.

    <html>
    <head>
    <title></title>
    <meta NAME="Generator" CONTENT="TextPad 4.6">
    <script language="javascript">
    var allOptions = document.getElementsByTagName('input')
    var totalScore = 0
    function fSelectOption(lOption){
    lOption.weight = lOption.value
    }
    function fShowResult(){
    for(var i=0;i<allOptions.length;i++){
    if(allOptions[i].checked){
    totalScore+=Math.round(allOptions[i].weight)
    }
    }
    document.getElementById('result').innerHTML="Total Score = "+totalScore
    }
    </script>
    </head>

    <body BGCOLOR="#FFFFFF">
    <input id="Radio1" type="radio" name="rblA" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio2" type="radio" name="rblA" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
    <input id="Radio3" type="radio" name="rblB" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio4" type="radio" name="rblB" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
    <input id="Radio5" type="radio" name="rblC" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio6" type="radio" name="rblC" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
    <input id="Radio7" type="radio" name="rblD" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio8" type="radio" name="rblD" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
    <br />
    <input type="button" value="submit" onclick="fShowResult()" />
    <div id="result" style="position:absolute; width:190px; height:20px; z-index:1; left: 200; top: 150; visibility: visible;"></div>
    </body>
    </html>

  3. #3
    Join Date
    Jan 2007
    Location
    Washington DC
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That is it, your are the man/lady Just one thing is there a way to do that dynamic as each radio button is check?

  4. #4
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hey there...I am not able to get you...please gime me more specifications..

  5. #5
    Join Date
    Jan 2007
    Location
    Washington DC
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok for example when every the Yes and No radio buttons holds a weight value for the question. So when the Yes is mark then it would put that question weight value in the TotalResults but if No is mark it will put a 0 value in the TotalResults. This would all be done in real time. So ever time a Yes it mark it would fire off the Sum and total up the All the Radio marked Yes and if no I mark I would Refire the Total to get new value.

  6. #6
    Join Date
    Jan 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi There,
    please check this out....
    ------------------------------------------------------------
    <html>
    <head>
    <title></title>
    <meta NAME="Generator" CONTENT="TextPad 4.6">
    <script language="javascript">
    var allOptions = document.getElementsByTagName('input')
    function fSelectOption(lOption){
    lOption.weight = lOption.value
    setTimeout("fShowResult()",100);
    }
    function fShowResult(){
    var totalScore = 0
    for(var i=0;i<allOptions.length;i++){
    if(allOptions[i].checked){
    totalScore+=Math.round(allOptions[i].weight)
    }
    }
    document.getElementById('result').innerHTML="Total Score = "+totalScore
    }
    </script>
    </head>

    <body BGCOLOR="#FFFFFF">
    <input id="Radio1" type="radio" name="rblA" value="10" onclick="fSelectOption(this);"/>Yes&nbsp;<input id="Radio2" type="radio" name="rblA" value="0" onclick="fSelectOption(this)"/>No<br /><br />
    <input id="Radio3" type="radio" name="rblB" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio4" type="radio" name="rblB" value="0" onclick="fSelectOption(this)"/>No<br /><br />
    <input id="Radio5" type="radio" name="rblC" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio6" type="radio" name="rblC" value="0" onclick="fSelectOption(this)"/>No<br /><br />
    <input id="Radio7" type="radio" name="rblD" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio8" type="radio" name="rblD" value="0" onclick="fSelectOption(this)"/>No<br /><br />
    <br />
    <!--input type="button" value="submit" onclick="fShowResult()" /-->
    <div id="result" style="position:absolute; width:190px; height:20px; z-index:1; left: 200; top: 150; visibility: visible;"></div>
    </body>
    </html>

  7. #7
    Join Date
    Jan 2007
    Location
    Washington DC
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That work great, thanks for you help with this Beeps

Bookmarks

Posting Permissions

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