Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery adding variables

Tags:

jquery

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);
like image 902
Rahul Singh Avatar asked Jan 27 '12 04:01

Rahul Singh


People also ask

How to add variable in JavaScript?

Add numbers in JavaScript by placing a plus sign between them. You can also use the following syntax to perform addition: var x+=y; The "+=" operator tells JavaScript to add the variable on the right side of the operator to the variable on the left.

How do you append to a variable?

Create an Append Variable activity with UI Select the Append Variable activity on the canvas if it is not already selected, and its Variables tab, to edit its details. Select the variable for the Name property. Enter an expression for the value, which will be appended to the array in the variable.


4 Answers

Cast them to numbers using parseInt or parseFloat:

var total_fee = parseInt(registration_fee) + parseInt(material_fee) + parseInt(tuition_fee);
like image 51
leepowers Avatar answered Nov 15 '22 07:11

leepowers


Try:


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

Or parseFloat, whichever suits

like image 40
Sudhir Bastakoti Avatar answered Nov 15 '22 08:11

Sudhir Bastakoti


Try

Cast them to numbers using Number

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

like image 35
riyas2806299 Avatar answered Nov 15 '22 06:11

riyas2806299


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

like image 33
xdazz Avatar answered Nov 15 '22 06:11

xdazz