I am trying to subtract a percentage from a number and have it display in an input using jQuery, but the form is pulling in values using PHP/MySQL.
I'm using an extra attribute on the select field because the value is a unique ID to be posted back to a different table on the database, this is what I have:
<input type="text" id="invamount" name="invamount" value="250.00"/>
<select id="invdiscount">
<option data-discount="0" value="" selected>No Discount</option>
<option data-discount="5" value="1">Discount 1 - 5%</option>
<option data-discount="10" value="2">Discount 2 - 10%</option>
<option data-discount="15" value="3">Discount 3 - 15%</option>
</select>
<input type="text" id="invtotal" name="invtotal" />
<script>
jQuery(document).ready(function() {
jQuery("#invtotal").on("click", function() {
sum();
});
});
function sum() {
var amount = document.getElementById('invamount').value;
var discount = jQuery("#invdiscount option").attr("data-discount");
var maths1 = (discount * amount/100);
document.getElementById('invtotal').value = maths1;
}
</script>
There are no errors in the console when loading the form but each time I select a different discount, the value of the total input just stays at 0.