/    Sign up×
Community /Pin to ProfileBookmark

Javascript total price

Hello,

I am making a price per seat map for a fictional website for my school.
The problem is that I need to let people see the total price but I don’t know how to do it.

<script type=”text/javascript”>
var totaal=0;
function fm10(){
var chkBox=document.getElementById(‘m10’);
if(chkBox.checked)
{
document.getElementById(‘tm10’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm10’).innerHTML = ”;

}

}

function fm9(){
var chkBox=document.getElementById(‘m9’);
if(chkBox.checked)
{
document.getElementById(‘tm9’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm9’).innerHTML = ”;

}

}

function fm8(){
var chkBox=document.getElementById(‘m8’);
if(chkBox.checked)
{
document.getElementById(‘tm8’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm8’).innerHTML = ”;

}

}

function fm7(){
var chkBox=document.getElementById(‘m7’);
if(chkBox.checked)
{
document.getElementById(‘tm7’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm7’).innerHTML = ”;

}

}

function fm6(){
var chkBox=document.getElementById(‘m6’);
if(chkBox.checked)
{
document.getElementById(‘tm6’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm6’).innerHTML = ”;

}

}

function fm5(){
var chkBox=document.getElementById(‘m5’);
if(chkBox.checked)
{
document.getElementById(‘tm5’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm5’).innerHTML = ”;

}

}

function fm4(){
var chkBox=document.getElementById(‘m4’);
if(chkBox.checked)
{
document.getElementById(‘tm4’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm4’).innerHTML = ”;

}

}

function fm3(){
var chkBox=document.getElementById(‘m3’);
if(chkBox.checked)
{
document.getElementById(‘tm3’).innerHTML = ‘€7,50’;

}else{
document.getElementById(‘tm3’).innerHTML = ”;

}

}

</script>
<div id=”zalen1plattegrond” <?php echo $zaal1hidden;?>>
<form id=”zaal1plattegrond” method=”post” action=”IDEAL_test/klantgegevens.php” >
<form id=”zaal1plattegrond” method=”post” action=”IDEAL_test/klantgegevens.php” >
<table width=”400″ >
<tr align=”center”>
<td></td>
<td></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m10″ onClick=”fm10()” name=”stoel[]” value=”cl3m10″ <?php if($rcl3m10==true){ echo $disable;}?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m9″ onClick=”fm9()” name=”stoel[]” value=”cl3m9″ <?php if($rcl3m9==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m8″ onclick=”fm8()” name=”stoel[]” value=”cl3m8″ <?php if($rcl3m8==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m7″ onclick=”fm7()” name=”stoel[]” value=”cl3m7″ <?php if($rcl3m7==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m6″ onclick=”fm6()” name=”stoel[]” value=”cl3m6″ <?php if($rcl3m6==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m5″ onclick=”fm5()” name=”stoel[]” value=”cl3m5″ <?php if($rcl3m5==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m4″ onclick=”fm4()” name=”stoel[]” value=”cl3m4″ <?php if($rcl3m4==true){ echo $disable;} ?>></td>
<td bgcolor=”#c77f68″><input type=”checkbox” id=”m3″ onclick=”fm3()” name=”stoel[]” value=”cl3m3″ <?php if($rcl3m3==true){ echo $disable;} ?>></td>
<td></td>
<td></td>
</tr> <!– M –>
<tr align=”center”>
This is what I have so far

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ziggyfishOct 11.2012 — A word of advice, name your elements and functions with English readable names. Like for example:

function fm3(){

var chkBox=document.getElementById('m3');

if(chkBox.checked)

{

document.getElementById('tm3').innerHTML = '€7,50';


}else{

document.getElementById('tm3').innerHTML = '';


}

}[/QUOTE]


should be:

function getTotalPrice(checkboxElement, priceElementID, amount){
var chkBox = document.getElementById(checkboxElementID);
if (chkBox.checked){
document.getElementById(priceElementID).innerHTML = '€' + amount;
} else {
document.getElementById(priceElementID).innerHTML = '';
}
}


repost it with code like that and you may get a better response, if not you have learned how to name variables and functions correctly. Not only does it help you follow what the code does, it also allows other people to easily follow and understand your code and give you feedback.
Copy linkTweet thisAlerts:
@BorderauthorOct 11.2012 — Thanks for your feedback.

And the reason I gave the element that name is because I need 1000 of those functions,

so I gave them the name of the seat.

And great thanks for your help.
Copy linkTweet thisAlerts:
@ziggyfishOct 11.2012 — Thanks for your feedback.

And the reason I gave the element that name is because I need 1000 of those functions,

so I gave them the name of the seat.

And great thanks for your help.[/QUOTE]


You would have noticed that the function I gave you allowed you to enter the checkbox element and the totalamount element as parameters. doing this allows you to have one function that does multiple things (i.e you don't need 1000 functions).

For example given that function your checkboxes boxes become:

[CODE]<td bgcolor="#c77f68"><input type="checkbox" id="m10" onClick="getTotalPrice('m10', 't10', '7,50')" name="stoel[]" value="cl3m10" <?php if($rcl3m10==true){ echo $disable;}?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m9" onClick="getTotalPrice('m9', 't9', '7,50')" name="stoel[]" value="cl3m9" <?php if($rcl3m9==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m8" onclick="getTotalPrice('m8', 't8', '7,50')" name="stoel[]" value="cl3m8" <?php if($rcl3m8==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m7" onclick="getTotalPrice('m7', 't7', '7,50')" name="stoel[]" value="cl3m7" <?php if($rcl3m7==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m6" onclick="getTotalPrice('m6', 't6', '7,50')" name="stoel[]" value="cl3m6" <?php if($rcl3m6==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m5" onclick="getTotalPrice('m5', 't5', '7,50')" name="stoel[]" value="cl3m5" <?php if($rcl3m5==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m4" onclick="getTotalPrice('m4', 't4', '7,50')" name="stoel[]" value="cl3m4" <?php if($rcl3m4==true){ echo $disable;} ?>></td>
<td bgcolor="#c77f68"><input type="checkbox" id="m3" onclick="getTotalPrice('m3', 't3', '7,50')" name="stoel[]" value="cl3m3" <?php if($rcl3m3==true){ echo $disable;} ?>></td>[/CODE]


HINT Doing something like this may lead you to the solution your after ?
×

Success!

Help @Border spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...