The Little Guy Posted June 9, 2008 Share Posted June 9, 2008 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 More sharing options...
KevinM1 Posted June 9, 2008 Share Posted June 9, 2008 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. Link to comment https://forums.phpfreaks.com/topic/109444-solved-add-not-concatenate/#findComment-561381 Share on other sites More sharing options...
Psycho Posted June 9, 2008 Share Posted June 9, 2008 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' } Link to comment https://forums.phpfreaks.com/topic/109444-solved-add-not-concatenate/#findComment-561573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.