Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem that I can't solve. I'm trying to make a code that make a sum of radio and checkbox buttons. When I select some choices in the checkbox selection it doesn't work how it should be.

------------------------------------------

HTML
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
        function DisplayPrice(price){
            var val1 = 0;
            for( i = 0; i < document.form1.price.length; i++ ){
                if( document.form1.price[i].checked == true ){
                    val1 = document.form1.price[i].value;
                }
            }

            var val2 = 0;
            for( i = 0; i < document.form2.price2.length; i++ ){
                if( document.form2.price2[i].checked == true ){
                    val2 = document.form2.price2[i].value;
                }
            }

	    var val3 = 0;
            for( i = 0; i < document.form3.price3.length; i++ ){
                if( document.form3.price3[i].checked == true ){
                    val3 = document.form3.price3[i].value;
                }
            }

            var sum=parseInt(val1) + parseInt(val2) + parseInt(val3);
            document.getElementById('totalSum').value=sum;
        }
    </script>
</head>
<body>
    <b><u>Group 1:</u></b>
    <br>
    <form name="form1" id="form1" runat="server">
        <br>
        <input id="rdo_1" type="radio" value="1000" name="price" onclick="DisplayPrice(this.value);">Choice 1 - € 1.000,00
        <br>
        <input id="rdo_2" type="radio" value="2000" name="price" onclick="DisplayPrice(this.value);">Choice 2 - € 2.000,00
        <br>
        <input id="rdo_3" type="radio" value="5000" name="price" onclick="DisplayPrice(this.value);">Choice 3 - € 5.000,00
        <br>
        <p></p>
    </form>

    <b><u>Group 2:</u></b>
    <br>
    <form name="form2" id="form2" runat="server">
        <br>
        <input id="rdo_1" type="radio" value="100" name="price2" onclick="DisplayPrice(this.value);">Choice 1 - € 100,00
        <br>
        <input id="rdo_2" type="radio" value="200" name="price2" onclick="DisplayPrice(this.value);">Choice 2 - € 200,00
        <br>
        <p></p>
    </form>

<b><u>Group 3:</u></b>
    <br>
    <form name="form3" id="form3" runat="server">
        <br>
        <input id="rdo_1" type="checkbox" value="100" name="price3" onclick="DisplayPrice(this.value);">Choice 1 - € 100,00
        <br>
        <input id="rdo_2" type="checkbox" value="200" name="price3" onclick="DisplayPrice(this.value);">Choice 2 - € 200,00
        <br>
	<input id="rdo_1" type="checkbox" value="450" name="price3" onclick="DisplayPrice(this.value);">Choice 3 - € 450,00
        <br>
	<input id="rdo_1" type="checkbox" value="600" name="price3" onclick="DisplayPrice(this.value);">Choice 4 - € 600,00
        <br>
	<input id="rdo_1" type="checkbox" value="750" name="price3" onclick="DisplayPrice(this.value);">Choice 5 - € 750,00
        <br>
        <p></p>
    </form>

<input type="text" name="totalSum" id="totalSum" value="" size="3" readonly="readonly">

</body>
</html>


What I have tried:

I tried to make different checkboxes but it didn't work so I tried to transform the checkbox buttons into radio buttons but it didn't work too...
Posted
Updated 22-Feb-18 0:10am
v3

1 solution

correction
var val3 = 0;
           for (i = 0; i < document.form3.price3.length; i++) {
               if (document.form3.price3[i].checked == true) {
                   val3 += parseInt( document.form3.price3[i].value);
               }
           }

Demo: Plunker[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900