Jump to content

[SOLVED] Add Not Concatenate


The Little Guy

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

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.