Jump to content

Recommended Posts

in the problem below, when the code gets to the "alert", it alerts the two, but as a concatenated string, and not a added integer.

 

the alert should show "261", but instead it shows "2538". How can I fix it?

 

function showInMap(coords,imageID){
var tag = document.getElementById('showTag');
tag.style.position = 'absolute';
tag.style.border = 'solid 2px #9a0000';
var left = document.getElementById(imageID).offsetLeft;
left = parseInt(left);
var top = document.getElementById(imageID).offsetTop;
tp = parseInt(top);
var nums = coords.split(',');		
alert(tp + nums[1] /* nums[1] equals 8*/);
tag.style.left = left+'px'
}

Link to comment
https://forums.phpfreaks.com/topic/109444-solved-add-not-concatenate/
Share on other sites

in the problem below, when the code gets to the "alert", it alerts the two, but as a concatenated string, and not a added integer.

 

the alert should show "261", but instead it shows "2538". How can I fix it?

 

function showInMap(coords,imageID){
var tag = document.getElementById('showTag');
tag.style.position = 'absolute';
tag.style.border = 'solid 2px #9a0000';
var left = document.getElementById(imageID).offsetLeft;
left = parseInt(left);
var top = document.getElementById(imageID).offsetTop;
tp = parseInt(top);
var nums = coords.split(',');		
alert(tp + nums[1] /* nums[1] equals 8*/);
tag.style.left = left+'px'
}

 

I have the feeling it's because 'coords' is a string, thus splitting it also results in a string.  Try running either the entire 'nums' array, or just 'nums[1]' through parseInt as well.

function showInMap(coords,imageID){
var tag = document.getElementById('showTag');
tag.style.position = 'absolute';
tag.style.border = 'solid 2px #9a0000';
var left = document.getElementById(imageID).offsetLeft;
left = parseInt(left);
var top = document.getElementById(imageID).offsetTop;
var nums = coords.split(',');		
alert(parseInt(top) + parseInt(nums[1]));
tag.style.left = left+'px'
}

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.