homepage Welcome to WebmasterWorld Guest from 54.166.110.222
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
total value when checkbox checked
David_M




msg:4305826
 10:53 am on Apr 29, 2011 (gmt 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.

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="">

 

rocknbil




msg:4306011
 5:22 pm on Apr 29, 2011 (gmt 0)

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.

David_M




msg:4306303
 12:45 pm on Apr 30, 2011 (gmt 0)

Worked! thanks!

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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved