javascript problem again, adding values up
Written By Sebastiano on Feb. 19, 2008.
8 Comments
Report Note
+ Clip This
okay here it is
var barPosition = 25;
document.getElementById("rightTextPosition").value = barPosition;
document.getElementById("rightTextPosition").value += 2;
end result of this is 252, and not 27 ....WHY ?!
and this
var barPosition = 25;
document.getElementById("rightTextPosition").value = barPosition;
document.getElementById("rightTextPosition").value -= 2;
end result is 23 and that is correct..
I probably know the answer somewhere in my messy head, BUT at the moment I have no clue....

Oli
Written Feb. 19, 2008 / Report /
Because when you pull it back from DOM it's a string. There's an Integer.parse(string) method, I think that should return the string as a proper integer.
This is what you get with weakly typed languages =)
Oli
Written Feb. 19, 2008 / Report /
My bad, the function definition is:
int parseInt(string)Sebastiano
Written Feb. 19, 2008 / Report /
oh great I'll try that, thanks Oli !
I just came up with this
document.getElementById("rightTextPosition").value = (barPosition * 1) + (2 * 1);and it works, but is not logic....
I'll go for your option ^_^
and god I hate JavaScript, but sometimes we just HAVE to use it, to get some interactive fast-response without (re)loading a page... beside of that i'm a php guru !
Oli
Written Feb. 19, 2008 / Report /
Yeah I understand. JS has its place in the world and you can do some pretty cool things with it but that doesn't stop it being a filthy, hacked-up language.
But as for your example:
var barPosition = 25,barElement = document.getElementById("rightTextPosition");barElement.value = barPosition;barElement.value = parseInt(barElement.value) - 2;I've cut out the duplicate getElementById because it's an expensive call. If you're referencing a DOM element over and over again, keep a pointer variable stored for it.
Mike
Written Feb. 19, 2008 / Report /
And just as a reference, if you have a number and you + "" to it, it automatically converts to a string.
Sebastiano
Written Feb. 20, 2008 / Report /
Oli:
Thanks for that tip, i'll keep it in mind!
Mike:
yep that I find-out the hard way! lol, anyway thanks for pointing it out.
so basically using pointer values shortens up your code, and when using numbers we have to use parseint() to convert string to integer.. ..
.......right? lol... I wanna learn, but i get confused with php functions and it's way of handling values
Oli
Written Feb. 21, 2008 / Report /
It not that not using two getElement calls shortens your code.
Finding a DOM element using
getElementById()requires the JS engine to parse the whole DOM and depending on the size of your page, this can take a relatively long time (a a lot of CPU cycles). Using it where you don't have to can cause noticeable lag for the end user, especially if their computer is slow.posure
Written Feb. 22, 2008 / Report /
Make sure that you specify the 10 radix for parseInt, i.e. parseInt("37", 10) otherwise it might parse incorrectly.