I am unable to find a way to total sales.
//Quarts 1
function updateOrder1() {
const X = 13;
var Y = document.getElementById("Quarts");
var value = Y[Y.selectedIndex].text;
var Z = value * X;
document.getElementById("total1").value = "$" + Z.toFixed(2)
}
//Pints 2
function updateOrder2() {
const X = 8;
var Y = document.getElementById("Pints");
var value = Y[Y.selectedIndex].text;
var Z = value * X;
document.getElementById("total2").value = "$" + Z.toFixed(2)
}
<table border=1 px>
<tr>
<td><b>Quarts</b></td>
<td>
<form>
<select id="Quarts" onchange="updateOrder1();">
<option>Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</td>
<td width="3%">
<input type="text" id="total1" name="total1" value="0" readonly="readonly" />
</td>
</tr>
<tr>
<td><b>Pints</b></td>
<td>
<form>
<select id="Pints" onchange="updateOrder2();">
<option>Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</td>
<td>
<input type="text" id="total2" name="total2" value="0" readonly="readonly" />
</td>
</tr>
<tr>
<td></td>
<td>
Total
</div>
</td>
<td></td>
</tr>
</table>
3
Answers
React to
change
events of your select boxes inside the table and useArray::reduce
to calculate the total, then insert it into the total cell:Here’s one approach. It’s best to reuse functions and pass in the value that changes rather than having separate functions for each case. You can update adjacent elements based on the event target’s position in the document rather than by ID.
Note that I’ve wrapped a single form around the entire table instead of having separate forms. Technically you don’t even need a form since you’re not submitting it anywhere.
<form>
the bees died* 1
various places to avoid parsing numbers