views:

763

answers:

2

Hi,

I have a code snippet that goes like this:

$('#content').css('padding-top', heightTwo + 44 + 'px');

heightTwo is always changing. When it has the value 200 for example, the padding top gets the value 20044. I want it to be 244 but don't know how to write it. What is the correct syntax?

Thanks

+3  A: 

Try

$('#content').css('padding-top', (parseInt(heightTwo, 10) + 44) + 'px');

The second argument to parseInt specifies that base 10 is to be used while parsing the number.

EDIT: The '+' operator concatenates if the operands are 'string' objects OR adds the operands if they are number. In your case, it is treating heightTwo as 'string' and doing concatenation. parseInt will convert heightTwo to an Integer and the plus operator will do addition.

SolutionYogi
Works like a charm, thanks a million times!
Fred Bergman
Mmm, I think it's the `'px'`, not the `heightTwo`, that's causing it to do string concatenation. Assuming `heightTwo` is always returning numerals, without the `+ 'px'` the code would do addition instead of string concatenation.
Val
A: 

heightTwo and 44 is numeric, you can close parentheses.

$('#content').css('padding-top', (heightTwo + 44) + 'px');