Welcome to WebmasterWorld Guest from 54.198.222.129

Forum Moderators: open

Message Too Old, No Replies

total value when checkbox checked

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

10+ Year Member



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.

eg
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);
}
</script>

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

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



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

Or, do it like this . . .

value="1:9.99"

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)

10+ Year Member



Worked! thanks!

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

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

Featured Threads

Hot Threads This Week

Hot Threads This Month