I have created a very simple table in HTML. The column "Sector" has 4 possible selections in a drop down list. I type manually the amount of money in the second column. What I want to do is to display Total Money in the third column, which should be:
Total money = (Amount of money) * 1, in case that Sector = Footbal (and therefore value "s1")
Total money = (Amount of money) * 9, in any other case
For some reason it multiplies always by 9. How can I make this work?
function getValue() {
var selectedValue1 = document.querySelectorAll(".sector");
if (selectedValue1 == 's1') {
CONSTANT = 1;
} else {
CONSTANT = 9;
}
}
var Initial = document.querySelectorAll('.number1');
var Double = document.querySelectorAll('.number2');
Initial.forEach(myFunction);
function myFunction(item, index) {
getValue()
item.addEventListener('change', (event) => {
var initialValue = event.target.value;
Double[index].value = initialValue * CONSTANT;
});
}
<! --COMIENZO TABLA -->
<table class="egt">
<! --SEGUNDA LINEA -->
<tr>
<th>Sector</th>
<th>Amount of money</th>
<th>Total money</th>
</tr>
<! --TERCERA LINEA -->
<tr>
<td>
<select class="sector">
<option value="s1">Football</option>
<option value="s2">Basketball</option>
<option value="s3">Volleyball</option>
<option value="s4">Rugby</option>
</select>
</td>
<td>
<input type="number" class="number1">
</td>
<td>
<input type="number" class="number2">
</td>
</tr>
<! --CUARTA LINEA -->
<tr>
<td>
<select class="sector">
<option value="s1">Football</option>
<option value="s2">Basketball</option>
<option value="s3">Volleyball</option>
<option value="s4">Rugby</option>
</select>
</td>
<td>
<input type="number" class="number1">
</td>
<td>
<input type="number" class="number2">
</td>
</tr>
</table>
<! --FINAL TABLA -->
