Jump to content

Cant use math addition on two variables.


UrbanDweller

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/259479-cant-use-math-addition-on-two-variables/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.