javascript - Add (or multiply, subtract or divide) one var to another in JS/jQuery -
sincere apologies if has been asked , answered elsewhere, struggled finding accurate search terms this.
working in jquery have var
called total
. need add value of other vars
total
. vars
in question have numerical values.
this i've tried:
var total = total+rocketspend; $(".totalamount").html(total);
and
var newtotal = total+rocketspend; var total = newtotal; $(".totalamount").html(total);
both have resulted in .totalamount
being emptied , replaced nothing. have theories around why might going wrong - in first example var
isn't allowed self defining, , in in second example browser attempts define both newtotal
, total
@ same time, ending in mystery. or else entirely. rocketspend
var
works fine on own, total
before attempted addition.
thoughts?
you need use var
when first define variable. looks you're trying access variable exists. example:
var total = 0; var rocketspend = 10; total = total + rocketspend; $(".totalamount").html(total);
additionally, try checking console errors. in browsers, can right click , inspect element or click ctrl + shift + i , clicking on console tab. can use ctrl + shift + k in firefox.
Comments
Post a Comment