Rahul Singh
Rahul Singh

Reputation: 1632

jQuery adding variables

I have used following code to add three variables but instead of adding these variables its concatenating these variables.

var registration_fee = $('input[type="radio"][name="registration_fee"]:checked').val();
var material_fee = $('input[type="radio"][name="material_fee"]:checked').val();
var tuition_fee = $('input[type="radio"][name="tuition_fee"]:checked').val();
// alert(tuition_fee)
var total_fee = registration_fee + material_fee + tuition_fee;
$('#total_fee').html(total_fee);

Upvotes: 6

Views: 31406

Answers (5)

subindas pm
subindas pm

Reputation: 2774

Try to use parseInt(price) + parseInt(ticket_buyer_fee) for the variables it works

Upvotes: 0

riyas2806299
riyas2806299

Reputation: 21

Try

Cast them to numbers using Number

tal_fee = Number(registration_fee) + Number(material_fee) + Number(tuition_fee);

Upvotes: 2

xdazz
xdazz

Reputation: 160843

Use parseInt to turn the string to int, or parseFloat for float.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:


var total_fee = parseInt(registration_fee, 10) + parseInt(material_fee, 10) + parseInt(tuition_fee, 10);

Or parseFloat, whichever suits

Upvotes: 2

leepowers
leepowers

Reputation: 38318

Cast them to numbers using parseInt or parseFloat:

var total_fee = parseInt(registration_fee) + parseInt(material_fee) + parseInt(tuition_fee);

Upvotes: 13

Related Questions