websmentor.com websmentor.com - 2 months ago 12
Javascript Question

Add two variable using jquery

Hi friends I have two variables price1 & price 2, I am getting price value dynamically using onchange event now i want to add these two variables.

//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
var price1 = 300;
});



$('.support-layer-thickness').on('change', function(e) {
//second price
var price2 = 200;

});


Now I want to add both variables price1 & price2
eg price = price1+price2;
o/p price= 500;


how can i achieve this..

Answer

you need to define global variable for those 2 function like

    var price=0,price1=0,price2=0;
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
      //first price
       price1 = 300;
       price=price1+price2;
       alert(price);
  });



 $('.support-layer-thickness').on('change', function(e) {
  //second price
  price2 = 200;
  price=price1+price2;
  alert(price);
 });