I have an html form with Sub-Total, Discount and Total. The select drop down for Discount has 5%, 10% and 20% values. The select drop down for Sub-Total has $200, $100 and $20 values. Another input text field gets the Total form a calculation on the Sub-Total and Discount.
Discount:
<select class="select" name="discount">
<option value="5" selected>5% discount</option>
<option value="10">10% discount</option>
<option value="20">20% discount</option>
</select>
Sub-Total:
<select class="select" name="discount">
<option value="100" selected>$100</option>
<option value="200">$200</option>
<option value="50">$50</option>
</select>
Total:
<input type="text" name="price" value="<?php echo $db['sellingprice'] ?>">
What I need is:
If the input Sub-Total has a value lets say $100 and the Discount to give the Sub-Total is 10% then the Total should change to $90.
Is there any way to update the total price in the input text box every time whenever the Subtotal Price or Discount values are changed?
the calculation to be done is:
total = subTotal - (subTotal * (discount / 100))
I was able to do something similar with .onmouseout
but it isn't working quite how I want it to.
<script>
var discount = 10;
document.getElementById("sellingprice").onmouseout = function(){
document.getElementById("price").value = parseFloat(document.getElementById("sellingprice").value) * (1.00 - discount)
}
</script>
here is a link to a jsfiddle if you would like to play with it yourself
Using
javascript
andhtml
only. This will update the textbox with the value selectedonchange
.By setting the
onchange
attribute on the select tag, you register theupdateInput()
function as a listener to theonchange
event.Then inside the function you can use
document.getElementsByName()
to access the elements and manipulate their values. Notice thatdocument.getElementsByName()
returns an array-like collection of elements, therefore requiring us to select the first element like soindex of the element we want to select goes here --------^
here is a link to a jsfiddle if you would like to play with it yourself