Xirlas
Xirlas

Reputation: 117

Use another attribute instead of the input value to update total

basically the below code creates a order total in the id="total" span depending on which checkbox/radio buttons are selected. The total is changed by the value of the checkbox/radio buttons however i was wondering if there's any other way to change the total without using the inputs value because its needed in the next part of the form. Maybe another input attribute? I'm just not sure how to go about it.

All help will be greatly appreciated, thank you!

<form action="cart.php" method="post" name="builder">
<input checked="checked" type="radio" name="term" value="12" onclick='check_value(this, 1)' />
<input type="radio" name="term" value="24" onclick='check_value(this, 2)' />
<input type="radio" name="term" value="36" onclick='check_value(this, 3)' />

<input type="checkbox" name="cid[]"  value="2" onclick='check_value(this, "")' />
<input type="checkbox" name="cid[]"  value="3" onclick='check_value(this, "")' />
<input type="checkbox" name="cid[]"  value="4" onclick='check_value(this, "")' />

Total Order: $<span id="total">36</span>
</form>

function check_value(curElem, id) {
            // calculate Total
            var total = 0;
            var controls = document.getElementsByTagName('input');
            for (var i = 0; i < controls.length; i++) {
                if ((controls[i].type === 'radio' || controls[i].type === 'checkbox') && controls[i].checked) {
                    total = total + parseFloat(controls[i].value);
                }
            }
            document.getElementById("total").innerHTML = total;
            //alert("Total: " + total);
        }  

Upvotes: 0

Views: 126

Answers (1)

manoj
manoj

Reputation: 3761

if you really want to use another attribute, use it

use an attribute of your choice, say nval and access it as controls[i].getAttribute("nval")

otherwise,

you can create plenty of hidden input fields in the page, for each checkbox to hold your value. and use these hidden fields to compute total.

Upvotes: 1

Related Questions