Firefog
Firefog

Reputation: 3174

How to calculate total price after discount input

Calculate total price after discount input OnChange not working

function CalcDiscount()
{
	var qty = document.getElementById("ticket-count").innerText;
	var value = document.getElementById("item-price").innerText;
	var discount = document.getElementById("discount").value;
	var total =  value * qty;
	var gtotal =  total - discount;
	//document.getElementById("total-price").value = gtotal;	
	$("#total-price").val(gtotal);
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Ticket Fare: <span id="item-price" class="item-price">450</span><br />
Ticket Number:<span class="ticket-count" id="ticket-count" > 5</span><br />

Discount: <input class="discount" id="discount" name="discount" type="text" onkeyup="CalcDiscount();"> <br /><br />
Total: <input class="total-price" id="total-price" name="totalprice" type="text" >

Upvotes: 0

Views: 3092

Answers (1)

Robert Columbia
Robert Columbia

Reputation: 6408

You need to convert your values from strings to numbers.

Try this:

function CalcDiscount()
{
    var qty = parseInt(document.getElementById("ticket-count").innerText);
    var value = parseInt(document.getElementById("item-price").innerText);
    var discount = parseInt(document.getElementById("discount").value);
    var total =  value * qty;
    var gtotal =  total - discount;
    //document.getElementById("total-price").value = gtotal;    
    $("#total-price").val(gtotal);
}

Upvotes: 1

Related Questions