Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

total value when checkbox checked

10:53 am on Apr 29, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Mar 7, 2003
posts: 301
votes: 0

Can anyone give a suggestion on how to change this code so that the value passed is not the value used in the check box value= statement? I need the value= to be a different thing for a php script.

instead of
<input type="checkbox" id='game0' value="9.99" onclick="UpdateCost()">Game 1 ( 9.99)<br>

i would have:
<input type="checkbox" id='game0' value="1" cost="9.99" onclick="UpdateCost()">Game 1 ( 9.99)<br>

Full sample script:

<script type="text/javascript">
// Created by: Jay Rumsey | http://www.nova.edu/~rumsey/
// This script downloaded from JavaScriptBank.com

function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<5; i++) {
gn = 'game'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
document.getElementById('totalcost').value = sum.toFixed(2);

<input type="checkbox" id='game0' value="9.99" onclick="UpdateCost()">Game 1 ( 9.99)<br>
<input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br>
<input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br>
<input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br>
<input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>
<input type="text" id="totalcost" value="">
5:22 pm on Apr 29, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

You could use some other valid attribute for the price, like title or rel?

Or, do it like this . . .


and have the Javascript split the values on the : for the 9.99, and the PHP script explode the values for the 1.
12:45 pm on Apr 30, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Mar 7, 2003
votes: 0

Worked! thanks!

added a title= and changed:
if (elem.checked == true) { sum += Number(elem.value); }

if (elem.checked == true) { sum += Number(elem.title); }

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members