How remove the mail subtotal if qty 0
To remove the mail subtotal if the quantity is 0, you can use a combination of JavaScript and CSS. Here's an example:
HTML:
<table>
<tr>
<td>Product</td>
<td>Quantity</td>
<td>Subtotal</td>
</tr>
<tr>
<td>Product 1</td>
<td><input type="number" value="0" /></td>
<td>$0.00</td>
</tr>
<tr>
<td>Product 2</td>
<td><input type="number" value="0" /></td>
<td>$0.00</td>
</tr>
</table>
CSS:
.subtotal {
display: none;
}
JavaScript:
const subtotalElements = document.querySelectorAll('.subtotal');
subtotalElements.forEach((element) => {
const quantityInput = element.previousElementSibling.querySelector('input');
if (parseInt(quantityInput.value) === 0) {
element.style.display = 'none';
}
});
This code selects all elements with the class subtotal
, then loops through each element and checks if the previous sibling element (which is the quantity input) has a value of 0. If it does, it sets the display property of the subtotal
element to none
, effectively hiding it.
You can also use a more concise version using the ternary operator
:
subtotalElements.forEach((element) => {
element.style.display = element.previousElementSibling.querySelector('input').value === '0'? 'none' : '';
});
This code does the same thing as the previous example, but in a single line.
Note that this code assumes that the subtotal
elements are direct children of the tr
elements, and that the quantity
inputs are direct siblings of the subtotal
elements. If your HTML structure is different, you may need to adjust the code accordingly.