I am having difficulties with the .js code. Basically, I want to be able to select an item from a list of arrays, get and display the unit price from a list in .js and be able to calculate the total price and pass that off to the payment processor. I’ve done my best. I need someone kind enough to look at my code and complete it so that I can learn from his/her example.
function createBill() {
let unitPrice = {
blueBRT: 80000,
redBRT: 50000,
coaster: 35000,
tata: 30000,
};
let totalPrice = {}
totalPrice.blueBRT = ($("#qty_blueBRT").val() * unitPrice.blueBRT)
$("#price_blueBRT").val(totalPrice.blueBRT);
totalPrice.redBRT = ($("#qty_redBRT").val() * unitPrice.redBRT)
$("#price_redBRT").val(totalPrice.redBRT);
totalPrice.coaster = ($("#qty_coaster").val() * unitPrice.coaster)
$("#price_coaster").val(totalPrice.coaster);
totalPrice.tata = ($("#qty_tata").val() * unitPrice.tata)
$("#price_tata").val(totalPrice.tata);
}
var e1 = document.getElementById("item1");
var itemselected1 = e1.options[e1.selectedIndex].value;
if (itemselected1 != "1") {
items[index] = itemselected1;
quantity[index] = document.getElementById("qty").value;
unitPrice[index] = document.getElementById("unitPrice").value;
index++;
}
var fTot = 0;
strt(1);
for (var i = 0; i < index; i++) {
document.write("<tr>");
createtbl(items[i]);
createtbl(quantity[i]);
createtbl(unitPrice[i]);
var totalPrice = parseInt(quantity[i]) * parseInt(unitPrice[i]);
document.write("<td>" + totalPrice + "</td>");
fTot += totalPrice;
document.write("</tr>");
}
document.write("<tr><td colspan=\"3\"><strong>TOTAL</strong></td><td>" + fTot + "</td><tr>");
strt(2);
function createtbl(x) {
document.write("<td>" + x + "</td>");
}
function strt(n) {
if (n == 1) {
document.writeln("<h1 style=\"text-align:center;\">The Bill</h1>");
document.writeln("<table width=\"90%\" border=\"1\">");
document.writeln("<tr><th>ITEMS</th><th>QUANTITY</th><th>PRICE</th><th></th></tr>");
} else
document.write("</table>");
}
function createTot(x, y) {
var tot = parseInt(x) * parseInt(y);
document.write("<td>" + tot + "</td>");
}
<form name="plform">
<table width="50%" id="alignment">
<tr>
<th> Choose Transport </th>
<th> Quantity </th>
<th> Cost per Unit </th>
<th> Total Cost </th>
</tr>
<tr>
<td>
<select id="item1">
<option value="1">--Select Bus--</option>
<option value="BLUE BRT BUS">BLUE BRT BUS WITH A/C (70 SEATERS)</option>
<option value="RED BRT BUS">RED BRT BUS WITH NO A/C (70 SEATERS)</option>
<option value="COASTER BUS">COASTER BUS 30 SEATERS</option>
<option value="TATA BUS">TATA BUS (28 SEATERS)</option>
</select>
</td>
<td><input type="number" value="0" id="qty"></td>
<td><input type="number" readonly value="0" id="unitPrice"></td>
<td><input type="number" readonly value="0" id="totalPrice"></td>
<td><input type="submit" class="button" value="Pay Now" onclick=createBill()></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</form>