Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: multiple calculation with different prices

1. ## multiple calculation with different prices

Hi
Im working on a form where the user can chose how many items the want.
most items cost 20 but some cost 35. so im trying to to make a script that will calculate the total numbers of item and the total price
items with pice of 30 have a class="mad1" and items with pice of 35 have a class="mad2" on the inputfields

so far i have come up with this :

Code:
```            var antal = 0;
var antalf = 0;
var antalr = 0;
var subtotal = 0;
var price = 30;
//add only if the value is number
if (this.value.trim().length != 0 && !isNaN(this.value.trim()) ) {
//Set max to 25
if (this.value >= 25){
this.value= 25
}
price = 35;
}
else{
price = 30;
}
antal += parseFloat(this.value);
subtotal = antal*price;
\$(this).css("background-color", "#ffffff");
}
else if (this.value.length != 0){
\$(this).css("background-color", "#e89898");
}
});
\$("#antal").html(antal.toFixed(0));
\$("#antalf").html(antalf.toFixed(0));
\$("#antalr").html(antalr.toFixed(0));
\$("#subtotal").html(subtotal.toFixed(0));```
But the price for all items is calculatede as if they are 35 ??
What am i doing wrong

The page im working on is Gordion - Online Bestilling

• Most of the things you say like
most items cost 20 but some cost 35.
and
items with price of 30 have a class="mad1" and items with price of 35 have a class="mad2"
don't appear to be true in your code here or on your page.
No Problem a subtotal can be gotten by grabbing the cost and multiplying it be the amount.
If you do not understand something in the code below feel free to ask. This has two functions, the first makes sure the user enters a number and the multiples this by the value in the price column.
The second function adds up the sub totals to keep a running total. This is fixed and drops down with the page.
A problem with the code is; it wont refigure the subtotal if you back space a number and delete the value there. I thought of how to do that
This show only part of your big table
Have fun:
Code:
```<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta charset="UTF-8" />
<title>Title of the document</title>
<style type="text/css">
#title{
position:fixed;
top:38px;
left:550px;
outline:1px solid black;
text-align:center;
height:8px;
width:65px;
}
#demo{
position:fixed;
top:60px;
left:550px;
outline:1px solid black;
text-align:center;
height:83px;
width:65px;
}
</style>
<body>
<table id="my_table" border="1">
<tr>
<td><h3 id="fugl">POULTRY</h3></td>
</tr>
<tr>
<td><h1>Cooked chicken</h1>
with pineapple, bacon and curry mayaise</td>
<td class="antal"><input  id="poulty_cooked_1" name="Cooked chicken on white bread" value="0" onkeyup="tally('poulty_cooked_1');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="number"></td>
<td class="antal"><input  id="poulty_cooked_2" name="Cooked chicken on rye bread" value="0" onkeyup="tally('poulty_cooked_2');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="number"></td>
<td id="poulty_cooked_price">30,-</td>
<td id="poulty_cooked_sub" style="width: 50px;">0</td>
</tr>
<tr>
<td><h1>Boiled chicken</h1>
with pineapple, bacon and mushrooms</td>
<td class="antal"><input  id="poulty_boiled_1" name="Boiled chicken on white bread" value="0" onkeyup="tally('poulty_boiled_1');" onfocus="this.value='';" style="width: 50px; background-color: rgb(255, 255, 255);" type="text"></td>
<td class="antal"><input  id="poulty_boiled_2" name="Boiled chicken on rye bread" value="0" onkeyup="tally('poulty_boiled_2');" onfocus="this.value=''" style="width: 50px; background-color: rgb(255, 255, 255);" type="text"></td>
<td id="poulty_boiled_price">30,-</td>
<td id="poulty_boiled_sub" style="width: 50px;">0</td>
</tr>
</table>
<div id="title" >Total Kr</div>
<div id="demo" ></div>

<script type='text/javascript' src='javascript/jquery.js'></script>
<script type="text/javascript">
var tot = 0;
var dolly = 0;
function tally(basic){
if(isNaN(document.getElementById(basic).value))
document.getElementById(basic).value = 0;
basic = basic.substring(0, basic.length-1);
cost = document.getElementById(basic+"price").innerHTML;
cost = Number(cost.substring(0, cost.length-2));
grand(basic);
}
function grand(place){
var x = document.getElementById("my_table").rows.length;
for(y=0; y<x;y++){
dolly += Number(document.getElementById("my_table").rows[y].cells[4].innerHTML);
if (isNaN(dolly)) dolly = 0;
}
document.getElementById("demo").innerHTML = "<b>"+dolly+",- Kr</b>";
dolly=0;
}
</script>
</body>
</html>```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•