javascriptvariables

Why isn't my JavaScript variable working?


This works:

$('.sameDiv').css('width', '25%');

But this doesn't:

var squaresize = 25;
$('.sameDiv').css('width', 'squaresize%');

Also, I've tried it with the percent symbol as part of the variable and that doesn't help.


Solution

  • $('.sameDiv').css('width', 'squaresize%');
    

    Should become

    $('.sameDiv').css('width', squaresize + '%');
    

    I did not test it, but if you just put 'squaresize%' it will not try to reference it... rather, just read it directly as a literal string evaluating to "squaresize%" instead of 25%