You need to modify your JavaScript to handle the Alert. I added a new condition inside your
keyuphandler
function to check if the current input elements belong to the
carbon
group and if the group is
1
. If this condition is met, it calculates the difference between
input1
and
input2
and displays an alert if the difference is greater than
0.3
-
<!DOCTYPE html>
<html>
<head>
<title>calculate</title>
</head>
<script>
const reducer = (a, c) => a + c;
const average = (n) => parseFloat(n.value) / 2 || 0;
const keyuphandler = (e) => {
if (e.target instanceof HTMLInputElement && e.target.type == 'text') {
let table = e.target.closest('table');
let tr = e.target.closest('tr');
let group = tr.dataset.group;
let elem = e.target.dataset.elem;
let result = table.querySelector
(`tr[data-group="${group}"] input[data-elem="${elem}"].result`);
let inputs = table.querySelectorAll
(`tr[data-group="${group}"] input[data-elem="${elem}"].input`);
result.value = [...inputs]
.map(average)
.reduce(reducer, 0);
if (elem === 'carbon' && group === '1') {
const input1 = parseFloat(inputs[0].value);
const input2 = parseFloat(inputs[1].value);
const difference = Math.abs(input1 - input2);
if (difference > 0.3) {
alert("NOT REPEATABLE.");
}
}
}
};
document.addEventListener('keyup', keyuphandler);
</script>
<style>
</style>
<body>
<table>
<!--
</table>
</body>
</html>
Your issue with using this inside the
keyuphandler
is that an alert will popup everytime a key is pressed until the full value has been entered. Not what you want. I have added the event to a button click, probably also not what you want, you need to change the code to do exactly what you require, this will point you in the right direction though:
<!DOCTYPE html>
<html>
<head>
<title>calculate</title>
</head>
<script>
const reducer = (a, c) => a + c;
const average = (n) => parseFloat(n.value) / 2 || 0;
const calculateAndCheckDifference = () => {
const table = document.querySelector('table');
const input1 = parseFloat(table.querySelector
('tr[data-group="1"] input[data-elem="carbon"][data-input="1"]').value);
const input2 = parseFloat(table.querySelector
('tr[data-group="1"] input[data-elem="carbon"][data-input="2"]').value);
const result = table.querySelector('tr[data-group="1"]
input[data-elem="carbon"].result');
result.value = average(table.querySelector('tr[data-group="1"]
input[data-elem="carbon"][data-input="1"]'));
const difference = Math.abs(input1 - input2);
if (difference > 0.3) {
alert("NOT REPEATABLE.");
}
};
</script>
<style>
</style>
<body>
<table>
<tr>
<th>%<br />Carbon</th>
<th>Average<br />Carbon</th>
</tr>
<tr data-group="1">
<td><input type="text" class="input"
data-elem="carbon" data-input="1" placeholder="input1" /></td>
<td><input type="text" class="result"
data-elem="carbon" readonly placeholder="average" /></td>
</tr>
<tr data-group="1">
<td><input type="text" class="input"
data-elem="carbon" data-input="2" placeholder="input2" /></td>
<td colspan="2"> </td>
</tr>
</table>
<button onclick="calculateAndCheckDifference()">Calculate and Check</button>
</body>
</html>
You can see a working fiddle on both:
- On using
keypress
- Keyuphandler[^]
- On using a
button
element - Using a button to calculate[^]