0

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);
        }  

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried you function in chrome 43, and it just worked, but you can getAttribute of 'value' rather than use non-standard attribute. i.e., controls[i].getAttribute('value').
He wanted to avoid using value, for reason that I don't know yet. So I suggested this answer
I actually need some more attributes to use like the nval and data attributes suggested. What else can i use? Can i make them up?
Yes you can, you can make your own values, as long as your browser supports it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.