Search code examples
javascriptcheckboxradio-buttoncalculator

Can I use radio buttons/checkboxes in place of textboxes in a Javascript calculator?


I have a Javascript calculator in which users can enter quantities of products/features, and it will multiply the quantity by the set price. Then, the result shows in a textbox below. My 2 questions are:

  1. I can use <select> options and specify a value for each one like this:

<select name="SITE_EM_4.99" onChange="CalculateTotal(this.form)"> <option value=""> - Select - </option> <option value="1">Yes</option> <option value="0">No</option> </select>

However, it won't let me do the same with checkboxes/radio buttons. How can I do that?

Two: Can I change the script so the total shows as actual text, vs in a text field? THANKS!

P.S. The script can be found HERE.


Solution

  • instead of having an input for the total just switch to a span (or something else) with an id (let's say "total"), and just replace it with innerHTML

    <script>
    function CalculateTotal(frm) {
    ...
    document.getElementById('total').innerHTML = round_decimals(order_total, 2);
    ...
    }
    </script>