UrbanDweller Posted March 22, 2012 Share Posted March 22, 2012 Hey, I have two variables which both hold different integer values that i would like to use addition on and make the answer into a new variable but due to js function that adds variables together with a + I cant seem to to use math addition on the two variables. Note: One variable is an array but in previous function subtraction worked fine in same situation Below is the snippet of the code that wont work. Thanks team function OnMouseDown(e){ imgDivPos = new imagePosition(imgDiv); mCurrentX = e.pageX + imgDivPos[0]; mCurrentY = e.pageY + imgDivPos[1]; alert(mCurrentY); cropDiv.onmousemove = OnMouseMove; } function imagePosition(param){ var x=0, y=0; x = param.offsetLeft; y = param.offsetTop; this[0] = x; this[1] = y; } Quote Link to comment https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/ Share on other sites More sharing options...
trq Posted March 22, 2012 Share Posted March 22, 2012 Where have you defined imgDiv? Quote Link to comment https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/#findComment-1330135 Share on other sites More sharing options...
UrbanDweller Posted March 22, 2012 Author Share Posted March 22, 2012 Thanks for the reply, All the variables have been called globally just to reduce clutter, but imgDiv is the name of the div i want to get the mouse xy position. Quote Link to comment https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/#findComment-1330137 Share on other sites More sharing options...
rythemton Posted March 22, 2012 Share Posted March 22, 2012 Subtract works fine because subtract only works on numbers, so JavaScript turns the values into numbers before subtracting. Unfortunately, Addition works as concatenate as well as addition, so if one variable is text (it may only do this if the first variable is text, but I'm not sure), it will convert both to text and concatenate them. Try using the parseInt() function to force both numbers to be Ints before adding them. Quote Link to comment https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/#findComment-1330248 Share on other sites More sharing options...
UrbanDweller Posted March 22, 2012 Author Share Posted March 22, 2012 Try using the parseInt() function to force both numbers to be Ints before adding them. worked like a charm, thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/#findComment-1330342 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.