Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.