I would like two things to happen.
One
when a selection is made the input field updates as the total,
Two
the check boxes are dynamically generated and may sometimes be up to
six and sometimes as low as one product.
This is what I have made, but I can’t get it to work 🙁
<script type="text/javascript">
function updateTotal(){
document.getElementById('total').value =
(document.getElementById('date0').checked?
parseFloat(document.getElementById('date0').price):0) +
(document.getElementById('date1').checked?
parseFloat(document.getElementById('date1').price):0) +
(document.getElementById('date2').checked?
parseFloat(document.getElementById('date2').price):0);
}
</script>
With this as form
<form>
<td id="datecontainer" onchange="Process(this.options[this.selectedIndex].value)">
<input id="date0" type="checkbox" name="form[date]" value="blue" price="10" onChange="javascript:updateTotal();">product 1<br />
<input id="date1" type="checkbox" name="form[date]" value="green" price="30" onChange="javascript:updateTotal();">product 2<br />
<input id="date2" type="checkbox" name="form[date]" value="red" price="50" onChange="javascript:updateTotal();">product 3<br />
</td>
<td>
Total cost is:
<input name="total" id="total" type="text" readonly="readonly" style="border:0px;">
</td>
</form>
Any help will be great!
You can do this by changing your code and markup as follows:
HTML
JS
A couple of things to note:
You do not need to include the javascript: before the function in the onchanged attribute.
You should prefix custom attributes with the data-* prefix.
You should surround your td tags with tr and table. The way that you are using them it would make more send to use an ul tag with li children.
DEMO – http://jsfiddle.net/9mxh5/
If the checkboxes are dynamically generated and you don’t know how many you may have:
HTML
JS
DEMO – http://jsfiddle.net/9mxh5/2/