Decaflon

Welcome to Decaflon! Where the geeks hang out: Signup or Login Here
Decaflon is proudly hosted by (mt) Media Temple.  We recommend them for your web hosting needs.
Clips: Popular Clips Upcoming Clips Notes: All Notes

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

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 =)

My bad, the function definition is: int parseInt(string)

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 !

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.

And just as a reference, if you have a number and you + "" to it, it automatically converts to a string.

Oli:

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.

Thanks for that tip, i'll keep it in mind!

Mike:

And just as a reference, if you have a number and you + "" to it, it automatically converts to a string.

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

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.

Make sure that you specify the 10 radix for parseInt, i.e. parseInt("37", 10) otherwise it might parse incorrectly.

Please Login To Leave A Comment

Decaflon Sponsors Get in touch if you want in.

Hot Notes (View all »)

 

Decaflon is part of the Chawlk Network of sites.

9 Great Places To Visit, Hang Out, & Meet New People

What's new and interesting at other Chawlk Network sites: