How to get the Same price for grouped items - javascript

This code works perfect for Items that have the same address.
If they have same addressId, then they are grouped together under that one address.
5 items same address that shows up fine, but it is placing the same shipping fee for each item.
Need to only show the same as address, only one shipping fee for Same Group of items.
Please help, I have been trying at this for past two-days..Cannot figure out how to return the one shipping fee for group of items.
var itemshippingGroup, should be grouped shipping fee, display, only one of those fees, not each 5-items as separate fee.
See code below
//cart rows
var numofItems = productCart.length;
var productSubTotal = 0, shipping = 0;
var retriveAdd = getStoredData(address_cookie_name);
//alert(retriveAdd);
var productAddress = $.parseJSON(retriveAdd);
var k = 0;
for(var ad in productAddress){
var j = 0;
for(var i in productCart) {
var ship = 0;
var value = productCart[i];
var addressId = productCart[i].addressId;
var shippingId = parseFloat(productCart[i].shippingmethod).toFixed(2);
if(ad == addressId){
var item = parseInt(i)+1;
var address = productAddress[productCart[i].addressId];
var itemshipping = parseFloat(productCart[i].shippingmethod).toFixed(2);
var fName = '', lName = '';
if(address){
fName = address.firstName;
lName = address.lastName;
itemAddress = address.address1+" <br> "+ address.city+","+address.state+" "+address.zipcode;
if(addressId == shippingId){
var itemshippingGroup = shippingId;
}
}
// Sub Total
productSubTotal = productSubTotal + ((parseFloat(value.productPrice) + parseFloat(value.productTax)) * value.productQty);
var trStyle = "style='background-color:#FFFFFF;'";
if(k%2 == 0){
trStyle = "style='background-color:#F1F1F1;'";
}
cart_html += '<tr id="cart_row_'+i+'" class="cart_row" '+trStyle+'>';
cart_html += '<td class="confirm-product"><span class="clearfix first_item last_item"><img src="'+value.productImage+'" alt="" title="'+value.productName+'" class="cart-images"></td>';
cart_html += '<td>'+value.productName+'<br>'+fName+' '+lName+'</td>';
if(j == 0){
cart_html += '<td>'+itemAddress+'</td>';
}else{
cart_html += '<td> </td>';
}
j++;
cart_html += '<td>'+currencySymbol+''+parseFloat(value.productPrice).toFixed(2)+'</td>';
cart_html += '<td>'+currencySymbol+''+parseFloat(value.productTax)+'</td>';
cart_html += '<td class="itemshipping">'+currencySymbol+' '+itemshipping+'</td>';
//cart_html += '<td>'+currencySymbol+' '+itemshipping+'</td>';
cart_html += '<td>'+currencySymbol+' '+ ((parseFloat(value.productPrice) + parseFloat(value.productTax))*value.productQty).toFixed(2)+'</td>';
//cart_html += '<td>×</td>';
cart_html += '</tr>';
checkButton(value.uProductID);
total_items += value.productQty;
shipping += parseFloat(productCart[i].shippingmethod);
}
}
k++;
}

Related

How can I replace an element in a cell using javascript? The json is from Progress-4GL DB

I'm new to both Progress and JavaScript. I'm getting elements from a temp-table in Progress using json. I'll show part of my code.
$.post({
url: gWS + "/bass/bass054.p",
data: {
"process": "<?php echo $process; ?>"
},
success: function(data){
var table = document.getElementById("table");
var j = 0;
var dataProc = "";
var qtProc = 0;
var item = "";
var sumQt = 0;
var qt_total = 0;
var idGrp = "";
for (var i = 0; i < data["tt-base"].length; i++) {
var row = table.insertRow(j+2);
if (data["tt-base"][i]["cod_id_bloco_edi"] != idGrp && i>=2) {
if (sumQt == qt_total) {
row.style.background = "green";
} else {
row.style.background = "#ff0000";
}
row.insertCell(0).innerHTML = "<b>QT TOTAL:</b>";
row.insertCell(1).innerHTML = "<b>" + item + "</b>";
row.insertCell(2).innerHTML = "<b>" + sumQt + "</b>";
row.insertCell(3).colSpan = "2";
row.insertCell(4).innerHTML = "<b>" + qt_total + "</b>";
sumQt = 0;
j++;
row = table.insertRow(j+2);
}
if (data["tt-base"][i]["cdn_segment_edi"] == 44) {
row.style.background = "#00ff00";
dataProc = data["tt-base"][i]["dsl_dados_entr_edi"].substring(7,9) + "/" + data["tt-base"][i]["dsl_dados_entr_edi"].substring(5,7) + "/20" + data["tt-base"][i]["dsl_dados_entr_edi"].substring(3,5) + "</div>";
qtProc = parseInt(data["tt-base"][i]["dsl_dados_entr_edi"].substring(11,20), 10) + "</div>";
row.insertCell(0).innerHTML = dataProc;
row.insertCell(1).innerHTML = qtProc;
row.insertCell(2).innerHTML = " ";
row.insertCell(3).colSpan = "2";
row.insertCell(4).innerHTML = "<div class='p6'> </div>";
j++;
sumQt = parseInt(sumQt,10) + parseInt(qtProc,10);
} else if (data["tt-base"][i]["cdn_segment_edi"] == 446) {
if (data["tt-base"][i]["dsl_dados_entr_edi"].indexOf("PRE") == 10) {
document.getElementById("table").deleteRow(j+1);
j--;
row = table.insertRow(j+2);
row.style.background = "#0099CC";
row.insertCell(0).innerHTML = dataProc;
row.insertCell(1).innerHTML = qtProc;
row.insertCell(2).innerHTML = "PREVIS";
row.insertCell(3).colSpan = "2";
row.insertCell(4).innerHTML = "<div class='p6'> </div>";
dataProc = '';
qtProc = 0;
j++;
} else if (data["tt-base"][i]["dsl_dados_entr_edi"].indexOf("FIR") == 10) {
document.getElementById("table").deleteRow(j+1);
j--;
row = table.insertRow(j+2);
row.style.background = "#00ff00";
row.insertCell(0).innerHTML = dataProc;
row.insertCell(1).innerHTML = qtProc;
row.insertCell(2).innerHTML = "FIRM";
row.insertCell(3).colSpan = "2";
row.insertCell(4).innerHTML = "<div class='p6'> </div>";
dataProc = '';
qtProc = 0;
j++;
}
}
idGrp = data["tt-base"][i]["cod_id_bloco_edi"];
}
}
}).fail(function() {
alert("Not Possible.");
})
So, instead of deleting the row as I'm doing it now I'd like to replace the previous only the cells with the FIRM and PREVIS. I tried using only row.insertCell(2).innerHTML = "PREVIS"; but it didn't work correctly.
Is there any suggestion as to how can I do it? Sorry if I'm not clear enough as i'm new to all this. Thanks for your time.
I managed to do what I wanted after tweaking for hours, lol.
Instead of using insertRow or insertCell, I just used the following
table.rows[j+2].style.background = "#0099CC";
table.rows[j+2].cells[2].innerHTML = "PREVIS";
This way, I was able to replace the content of what I wanted. I still have to change some other details, but the most difficult part is finally gone. Sorry if I wasted your time, but writing here has helped me to see the code in a different angle. I hope my code can help other people.

How can I calculate the average of the random numbers in a grid?

I have a random grid generator that asks how big you want the grid. If you input 5 it will produce a 5x5 grid with random numbers in it, or a 10x10 grid with random numbers etc.
I now need to take those random numbers once the grid is produced and display the average of the random numbers. I can't seem to get this part to work, because there are different numbers each time you generate a new grid.
var button = document.getElementById('gridSize');
button.onclick = function(e) {
result = document.getElementById('wrapper');
num = parseInt(document.getElementById('grid').value);
var str = "<table border='2'>";
for (row = 0; row < num; row++) {
str += "<tr>";
for (col = 0; col < num; col++) {
var randNumber = Math.floor(Math.random() * 100) + 1;
if (randNumber % 3 === 0) {
str += '<td style="background: red;">';
}
else if (randNumber % 2 === 0 && !randNumber % 3 === 0) {
str += '<td style="background: blue;">';
}
else {
str += "<td>";
}
str += randNumber + "</td>";
}
str += "</tr>";
}
str = str + "</table>";
result.innerHTML = str;
}
<form name="tablegen">
<input type="text" name="grid" id="grid"/>
<input name="generate" id="gridSize" type="button" value="Generate Grid!" onclick='createTable();'/>
</form>
<div id="wrapper"></div>
This is my code and I just can't seem to figure out how to go about this.
Here is a simple way, just accumulate the randNumber you calculated, then divide by the total number of cells.
var button = document.getElementById('gridSize');
button.onclick = function(e) {
result = document.getElementById('wrapper');
num = parseInt(document.getElementById('grid').value);
var avg = 0;
var str = "<table border='2'>";
for (row = 0; row < num; row++) {
str += "<tr>";
for (col = 0; col < num; col++) {
var randNumber = Math.floor(Math.random() * 100) + 1;
avg += randNumber;
if (randNumber % 3 === 0) {
str += '<td style="background: red;">';
}
else if (randNumber % 2 === 0 && !randNumber % 3 === 0) {
str += '<td style="background: blue;">';
}
else {
str += "<td>";
}
str += randNumber + "</td>";
}
str += "</tr>";
}
avg /= num*num;
str = str + "</table>" + "<span>Average is " + avg + "</span>";
result.innerHTML = str;
}
<form name="tablegen">
<input type="text" name="grid" id="grid"/>
<input name="generate" id="gridSize" type="button" value="Generate Grid!" onclick='createTable();'/>
</form>
<div id="wrapper"></div>
A couple of additions to what you gave us to start with.
var button = document.getElementById('gridSize');
var total = 0;
var divisor = 0;
button.onclick = function(e) {
result = document.getElementById('wrapper');
num = parseInt(document.getElementById('grid').value);
var str = "<table border='2'>";
for (row = 0; row < num; row++) {
str += "<tr>";
for (col = 0; col < num; col++) {
var randNumber = Math.floor(Math.random() * 100) + 1;
total = total+randNumber;
++divisor;
if (randNumber % 3 === 0) {
str += '<td style="background: red;">';
}
else if (randNumber % 2 === 0 && !randNumber % 3 === 0) {
str += '<td style="background: blue;">';
}
else {
str += "<td>";
}
str += randNumber + "</td>";
}
str += "</tr>";
}
str = str + "</table>";
result.innerHTML = str;
var average = total / divisor;
var averageSpan = document.getElementById('average');
averageSpan.innerText = average;
}
<form name="tablegen">
<input type="text" name="grid" id="grid"/>
<input name="generate" id="gridSize" type="button" value="Generate Grid!" onclick='createTable();'/>
</form>
Average: <span id="average"></span>
<div id="wrapper"></div>
Also a fiddle: https://jsfiddle.net/xpvt214o/775850/
Well, create a variable where you keep summing all generated random numbers.
At the end, get that number and divide by the area of the grid (the number user typed * the number user typed num*num)
Like below:
var button = document.getElementById('gridSize');
button.onclick = function(e) {
var total = 0;
result = document.getElementById('wrapper');
num = parseInt(document.getElementById('grid').value);
var str = "<table border='2'>";
for (row = 0; row < num; row++) {
str += "<tr>";
for (col = 0; col < num; col++) {
var randNumber = Math.floor(Math.random() * 100) + 1;
if (randNumber % 3 === 0) {
str += '<td style="background: red;">';
}
else if (randNumber % 2 === 0 && !randNumber % 3 === 0) {
str += '<td style="background: blue;">';
}
else {
str += "<td>";
}
total += parseInt(randNumber);
str += randNumber + "</td>";
}
str += "</tr>";
}
str = str + "</table>";
result.innerHTML = str;
console.log("Avg.: ", total/(num*num) || 0)
}
<form name="tablegen">
<input type="text" name="grid" id="grid"/>
<input name="generate" id="gridSize" type="button" value="Generate Grid!" onclick='createTable();'/>
</form>
<div id="wrapper"></div>
There are already some good answers. Here is a different approach. I also "cleaned up" the HTML and code a bit:
let button = document.getElementById("generatorButton");
button.onclick = function(e) {
document.getElementById("grid").innerHTML = createGrid();
document.getElementById("full-average").innerText = getGridAverage();
document.getElementById("mod3-average").innerText = getGridAverage(".mod3");
document.getElementById("mod2-average").innerText = getGridAverage(".mod2");
}
function createGrid()
{
gridSize = parseInt(document.getElementById('gridSize').value);
let str = "<table border='2'>";
for (let row = 0; row < gridSize; row++) {
str += "<tr>";
for (let col = 0; col < gridSize; col++) {
var randNumber = Math.floor(Math.random() * 100) + 1;
if (randNumber % 3 === 0) {
str += '<td class="mod3">';
}
else if (randNumber % 2 === 0) {
str += '<td class="mod2">';
}
else {
str += "<td>";
}
str += randNumber + "</td>";
}
str += "</tr>";
}
str = str + "</table>";
return str;
}
function getGridAverage(cssClass)
{
if (cssClass == undefined) cssClass = "";
let cells = document.querySelectorAll("#grid td" + cssClass);
if (cells.length > 0)
{
let sum = 0;
for (const cell of cells)
{
sum += parseInt(cell.innerHTML);
}
return sum / cells.length;
}
else
return 0;
}
.mod3 {
background-color: red;
}
.mod2 {
background-color: blue;
}
<form name="tablegen">
<input type="text" id="gridSize" placeholder="Enter grid size (e.g.: 5)" />
<input type="button" id="generatorButton" value="Generate Grid!" />
</form>
<div id="grid"></div>
<div>Full Average:<span id="full-average">n/a</span></div>
<div>Mod3 (red) Average:<span id="mod3-average">n/a</span></div>
<div>Mod2 (blue) Average:<span id="mod2-average">n/a</span></div>

How can I change the value of initialValue after each run?

I want to change the value of initialValue after each run Ex: If I type 1000, this will give the output as 11,000 (10000 + 1,000), and I minus and I type 2000, this will give the output as 9,000 (11,000 - 2,000). Can somebody help me regarding to my problem.
function Compute(initialNum, numOne) {
this._initialNum = 10000;
this._numOne = numOne;
this.addNum = function() {
this._initialNum = +this._initialNum + +this._numOne;
return this._initialNum;
};
this.minusNum = function() {
this._initialNum = +this._initialNum - +this._numOne;
return this._initialNum;
};
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="hidden" id="persistedResult" value="10000" /><br><br>
<input type="radio" id="rdoAdd" name="rdo">Add<br><br>
<input type="radio" id="rdoMinus" name="rdo">Minus<br><br>
<input type="text" id="txtNumOne"><br><br>
<button onclick="printResult()">Compute</button><br><br>
<table border="1px">
<th>Result</th>
<tbody id = "tblResult">
</tbody>
</table>
<script src="java.js"></script>
<script>
var tblResult = document.getElementById("tblResult");
var personList = [];
function printResult() {
var display = "";
var initialValue = parseInt(document.getElementById("persistedResult").value);
//var objAccount = new Compute(initialValue, numOne);
var rdoAdd = document.getElementById("rdoAdd");
var rdoMinus = document.getElementById("rdoMinus");
var numOne = parseInt(document.getElementById('txtNumOne').value);
//var numTwo = parseInt(document.getElementById('txtNumTwo').value);
var objCompute = new Compute(initialValue, numOne);
personList.push(objCompute);
console.log(personList);
var newValue = 0;
for(var i = 0; i < personList.length; i++) {
if(rdoAdd.checked) {
//display += objAccount.addNum();
newValue = personList[i].addNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
} else if(rdoMinus.checked){
//display += objAccount.minusNum();
newValue = personList[i]. minusNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
}
}
document.getElementById("persistedResult").value = newValue;
}
function resetx() {
document.getElementById('txtNumOne').value = "";
document.getElementById("rdoAdd").checked = false;
document.getElementById("rdoMinus").checked = false;
}
</script>
</body>
</html>
I want to change the value of initialValue after each run Ex: If I type 1000, this will give the output as 11,000 (10000 + 1,000), and I minus and I type 2000, this will give the output as 9,000 (11,000 - 2,000). Can somebody help me regarding to my problem.
//constructor function
function Compute(initialNum, numOne) {
this._initialNum = 10000;
this._numOne = numOne;
this.addNum = function() {
this._initialNum = +this._initialNum + +this._numOne;
return this._initialNum;
};
this.minusNum = function() {
this._initialNum = +this._initialNum - +this._numOne;
return this._initialNum;
};
}
//javascript in the body tag
var tblResult = document.getElementById("tblResult");
var personList = [];
function printResult() {
var display = "";
var initialValue = parseInt(document.getElementById("persistedResult").value);
var rdoAdd = document.getElementById("rdoAdd");
var rdoMinus = document.getElementById("rdoMinus");
var numOne = parseInt(document.getElementById('txtNumOne').value);
var objCompute = new Compute(initialValue, numOne);
personList.push(objCompute);
console.log(personList);
var newValue = 0;
for(var i = 0; i < personList.length; i++) {
if(rdoAdd.checked) {
newValue = personList[i].addNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
} else if(rdoMinus.checked){
newValue = personList[i]. minusNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
}
}
document.getElementById("persistedResult").value = newValue;
}
function resetx() {
document.getElementById('txtNumOne').value = "";
document.getElementById("rdoAdd").checked = false;
document.getElementById("rdoMinus").checked = false;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="hidden" id="persistedResult" value="10000" /><br><br>
<input type="radio" id="rdoAdd" name="rdo">Add<br><br> //rdo for Add
<input type="radio" id="rdoMinus" name="rdo">Minus<br><br> //rdo for Subs
<input type="text" id="txtNumOne"><br><br>
<button onclick="printResult()">Compute</button><br><br>
<table border="1px">
<th>Result</th>
<tbody id = "tblResult">
</tbody>
</table>
</body>
</html>
I want to change the value of initialValue after each run Ex: If I type 1000, this will give the output as 11,000 (10000 + 1,000), and I minus and I type 2000, this will give the output as 9,000 (11,000 - 2,000). Can somebody help me regarding to my problem.
function printResult() {
var display = "";
var initialValue = 10000;
//var objAccount = new Compute(initialValue, numOne);
var rdoAdd = document.getElementById("rdoAdd");
var rdoMinus = document.getElementById("rdoMinus");
var numOne = parseInt(document.getElementById('txtNumOne').value);
//var numTwo = parseInt(document.getElementById('txtNumTwo').value);
var objCompute = new Compute(initialValue, numOne);
personList.push(objCompute);
console.log(personList);
for(var i = 0; i < personList.length; i++) {
if(rdoAdd.checked) {
//display += objAccount.addNum();
display = "<tr>";
display += "<td>" + (personList[i].addNum()) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
} else if(rdoMinus.checked){
//display += objAccount.minusNum();
display = "<tr>";
display += "<td>" + (personList[i].minusNum()) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
}
}
}
//Constructor Function
function Compute(initialNum, numOne) {
this._initialNum = initialNum;
this._numOne = numOne;
this.addNum = function() {
this._initialNum += this._numOne;
return this._initialNum;
};
this.minusNum = function() {
this._initialNum -= this._numOne;
return this._initialNum;
};
}
Since HTTP is stateless and you want to perform operations on 2 different actions, you will need to store the result of each action in some place. For the simplest example, you can create hidden field on your HTML page. This can have value of 10000 when your HTML is loaded for the first time and update it’s value after each action is executed.
Let’s assume you have following hidden field on your HTML:
<input type=“hidden” id=“persistedResult” value=“10000” />
Here is your updated printResult method:
function printResult() {
var display = "";
var initialValue = parseInt(document.getElementById(‘persistedResult’).value);
//var objAccount = new Compute(initialValue, numOne);
var rdoAdd = document.getElementById("rdoAdd");
var rdoMinus = document.getElementById("rdoMinus");
var numOne = parseInt(document.getElementById('txtNumOne').value);
//var numTwo = parseInt(document.getElementById('txtNumTwo').value);
var objCompute = new Compute(initialValue, numOne);
personList.push(objCompute);
console.log(personList);
var newValue = 0;
for(var i = 0; i < personList.length; i++) {
if(rdoAdd.checked) {
//display += objAccount.addNum();
newValue = personList[i].addNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
} else if(rdoMinus.checked){
//display += objAccount.minusNum();
newValue = personList[i]. minusNum();
display = "<tr>";
display += "<td>" + (newValue) + "</td>";
display += "<tr>";
tblResult.innerHTML += display;
resetx();
}
}
document.getElementById(‘persistedResult’).value = newValue;
}
Note: This is a primitive example to guide you how to achieve data persistence. Generally, the data will be persisted on server.
This code takes care of scenario where there is only one person in the list, which is the approach in your current code example. You will need to enhance the logic if you want to have this working on an array of persons.
var valueEl = document.getElementById('value');
var addEl = document.getElementById('add');
var resultEl = document.getElementById('result');
var calculateEl = document.getElementById('calculate');
var initialValue = 1000;
function appendResult(result) {
var liEl = document.createElement("li");
liEl.innerHTML = result;
resultEl.appendChild(liEl);
}
function handleClickCalculate() {
var operand = addEl.checked ? 1 : -1;
var value = +valueEl.value * operand;
initialValue += value
appendResult(initialValue);
}
calculateEl.addEventListener('click', handleClickCalculate)
appendResult(initialValue);
jsFiddle

Javascript Discount Help Needed

I have this Javascript function that the discount applies to either FlavorBlue or FlavorRed. I tried to add the quantities together, but it did not work as expected. The goal was to have a 50% for quantity 2 flavors either blue or red. I have searched this site for help until blue and red in the face... Any insight would be extremely valued. Thanks in advance.
.js file:
function ordertotals()
{
var FlavorBluetotal = (order.selectFlavorBlueQuantity.value *
FlavorBlueprice.value);
document.getElementById('FlavorBluetotal').innerHTML = FlavorBluetotal;
order.FlavorBluetotalvalue.value = FlavorBluetotal;
var FlavorRedtotal = (order.selectFlavorRedQuantity.value *
FlavorRedprice.value);
document.getElementById('FlavorRedtotal').innerHTML = FlavorRedtotal;
order.FlavorRedtotalvalue.value = FlavorRedtotal;
var FlavorBluecasetotal = (order.selectFlavorBlueCaseQuantity.value *
FlavorBluecaseprice.value);
document.getElementById('FlavorBluecasetotal').innerHTML =
FlavorBluecasetotal;
order.FlavorBluecasetotalvalue.value = FlavorBluecasetotal;
// var total = FlavorBluetotal + FlavorRedtotal + FlavorBluecasetotal;
// document.getElementById('ordertotal').innerHTML = total;
// order.ordertotalvalue.value = total;
var totala = 0;
var totalb = 0;
var totalc = 0;
var quantity = (order.selectFlavorBlueQuantity.value +
order.selectFlavorRedQuantity.value);
// document.getElementById("insert").innerHTML = quantity;
if (order.selectFlavorBlueQuantity.value > 1) {
quantitya = (order.selectFlavorBlueQuantity.value);
var price = 29;
if (quantitya % 2 == 0) {
//calculate here buy one get one half price
real = ((quantitya/2)*price);
half = ((quantitya/2)*(price/2));
totala = (real+half);
} else {
quantitya = (quantitya-1);
real = ((quantitya/2)*price);
half = ((quantitya/2)*price/2);
totala = (real+half+price);
}
} else {
if (order.selectFlavorRedQuantity.value > 1) {
quantityc = (order.selectFlavorRedQuantity.value);
var price = 29;
if (quantityc % 2 == 0) {
//calculate here buy one get one half price
real = ((quantityc/2)*price);
half = ((quantityc/2)*(price/2));
totalc = (real+half);
} else {
quantityc = (quantityc-1);
real = ((quantityc/2)*price);
half = ((quantityc/2)*price/2);
totalc = (real+half+price);
}
}
}
if (order.selectFlavorBlueQuantity.value == 1 &&
order.selectFlavorRedQuantity.value == 1) {
var quantityb = 2;
var price = 29;
if (quantityb % 2 == 0) {
//calculate here buy one get one half price
real = ((quantityb/2)*price);
half = ((quantityb/2)*(price/2));
totalb = (real+half);
} else {
quantityb = (quantityb-1);
real = ((quantityb/2)*price);
half = ((quantityb/2)*price/2);
totalb = (real+half+price);
}
}
// if (order.selectFlavorBlueQuantity.value == 1 ||
order.selectFlavorRedQuantity.value == 1) {
// var quantityd = 1;
// var price = 29;
// totald = (quantityd * price);
// } else {
// }
// total = (totala + totalb + totalc + totald + FlavorBluecasetotal);
total = (totala + totalb + totalc + FlavorBluecasetotal);
document.getElementById('ordertotal').innerHTML = total;
order.ordertotalvalue.value = total;
}
php file:
//Inventory
echo "<tr><td colspan='2'><u>Select Inventory:</u></td></tr>";
//Quantity Flavor Blue
echo "<tr><td>Quantity of Flavor Blue:</td><td>";
echo "<label>$</label><input type='text' id='FlavorBlueprice'
name='FlavorBlueprice' maxlength='4' value='$FlavorBlueprice'
style='width:20px;'> <label>X</label> ";
echo "<input type='text' id='selectFlavorBlueQuantity'
name='selectFlavorBlueQuantity' maxlength='4'
value='$selectFlavorBlueQuantity' onchange='ordertotals();'
style='width:20px;'>";
echo "</select><label id='asterisk'> *</label>";
echo " <label>=</label> <label>$</label><label
id='FlavorBluetotal'></label>";
echo "</td></tr>";
//Quantity Flavor Red
echo "<tr><td>Quantity of Flavor Red:</td><td>";
echo "<label>$</label><input type='text' id='FlavorRedprice'
name='FlavorRedprice' maxlength='10' value='$FlavorRedprice'
style='width:20px;'> <label>X</label> ";
echo "<input type='text' id='selectFlavorRedQuantity'
name='selectFlavorRedQuantity' maxlength='4'
value='$selectFlavorRedQuantity' onchange='ordertotals();'
style='width:20px;'>";
echo "</select><label id='asterisk'> *</label>";
echo " <label>=</label> <label>$</label><label
id='FlavorRedtotal'></label>";
echo "</td></tr>";
//Quantity Flavor Blue
echo "<tr><td>Quantity of Flavor Blue Case:</td><td>";
echo "<label>$</label><input type='text' id='FlavorBluecaseprice'
name='FlavorBluecaseprice' maxlength='10' value='$FlavorBluecaseprice'
style='width:20px;'> <label>X</label> ";
echo "<input type='text' id='selectFlavorBlueCaseQuantity'
name='selectFlavorBlueCaseQuantity' maxlength='4'
value='$selectFlavorBlueCaseQuantity' onchange='ordertotals();'
style='width:20px;'>";
echo "</select><label id='asterisk'> *</label>";
echo " <label>=</label> <label>$</label><label
id='FlavorBluecasetotal'></label>";
echo "</td></tr>";
//Total
echo "<tr><td>Total:</td><td><label>$</label><label id='ordertotal'></td>
</tr>";
echo "<tr><td colspan='2'><hr></td></tr>";
I was just mistakenly had if else nested inside another. simple fix.

Total required in javascript

I have a query in javascript.. Please check the below image
In Above image :
First Input Box is Description
2nd Input Box is Qty
3rd is Value
I have got total qty using below script onchange of textbox :
function findTotal(){
var total = 0;
var $changeInputs = $('input.qtyValue');
$changeInputs.each(function(idx, el) {
total += Number($(el).val());
});
$('.total').text(total);
$("#totalvval").val(total);
}
I have got total Value using below :
function qfindTotal(){
var total = 0;
var $changeInputs = $('input.qqtyValue');
$changeInputs.each(function(idx, el) {
total += Number($(el).val());
});
$('.qtotal').text(total);
$("#totalqval").val(total);
if(total>10000){
alert("Amount should not be greater than 10000");
}
}
My query is that we need total of qty x value + qty x value + qty x value =total
You can try something like this:
Fiddle
Code
function createHTML() {
var html = "";
for (var i = 0; i < 3; i++) {
html += "<input type='text' class='qty' id='txtQty_" + i + "' onblur='updateTotal()' />";
html += "<input type='text' class='cost' id='txtQty_" + i + "' onblur='updateTotal()'/>";
html += "<br/>"
}
html += "Qty Total: <span id='qty_total'>0</span>";
html += "Cost Total: <span id='cost_total'>0</span>";
document.getElementById("content").innerHTML = html
}
function updateTotal() {
var qty = document.getElementsByClassName("qty");
var cost = document.getElementsByClassName("cost");
var total_qty = 0;
var total_cost = 0;
for (var i = 0; i < qty.length; i++) {
if (qty[i].value && cost[i].value) {
total_cost += qty[i].value * cost[i].value;
total_qty += parseInt(qty[i].value);
}
}
document.getElementById("qty_total").innerHTML = total_qty;
document.getElementById("cost_total").innerHTML = total_cost;
}
(function() {
createHTML();
})()
<div id="content"></div>

Categories

Resources