Daniel
Daniel

Reputation: 215

How can I subtract one variable from another?

I have this piece of jquery code:

jQuery(document).ready(function($) {
    var totaalBreedte = $("#menuus").width();
    var menuBreedte = $("#eersteMenu").width();
    var over = totaalBreedte - menuBreedte;

    alert(over);  
});

For example, when totaalBreedte = 800 and menuBreedte = 400. I can sum the two values, which alerts 1200, but when i substract them like above I recieve 0. I tried to cast them as a number and tried parseInt() but this doesn't make any difference. Someone who knows what i'm missing here?

Upvotes: 1

Views: 24966

Answers (2)

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

your code is working perfectly. I have just changed the element selectors for width to a simple int. And it worked correctly. Here is the fiddle:

http://jsfiddle.net/afzaal_ahmad_zeeshan/KnbEh/

And here is a snapshot;

enter image description here

You can see the value of the result being sent to alert. What you should do is to check the values for them. As @Vega has already mentioned, it looks like they are both 600 which is why, their sum is 1200, and when you subtract, its a 0. You might want to recheck the values.

Upvotes: 5

franzbischoff
franzbischoff

Reputation: 170

Did you tried to look into the actual value of both totaalBreedte and menuBreedte? Tried another browser? You can use for example the Chrome's console to try some JavaScript lines and debug this. Your code seems ok.

Upvotes: 0

Related Questions