0

I'm trying to dynamically calculate the min-width of body based on the number of div.columns that exist on the page, plus a padding of 180px. This is the code I'm using:

function countColumns() {
    var columns =  $('.column').size(); 
    document.write(columns)
}

$('body').css('min-width', (parseInt(columns) * 120) + 180 + 'px') );

Any clue as to why its not working?

2
  • 1
    columns is local to countColumns(), so you cannot access it from the outside. Also, your call to parseInt() looks unnecessary since size() already returns an int (and you should use length instead, btw). Commented Dec 1, 2011 at 16:43
  • 1
    ...and as if that weren´t enough, you never use countColumns() Commented Dec 1, 2011 at 16:50

4 Answers 4

6

This is due to the scope of the columns variable. You need to either make it global, or assign it to the result of the function, the latter being preferable. Try this:

function countColumns() {
    return $('.column').size(); 
}

$('body').css('min-width', (parseInt(countColumns()) * 120) + 180 + 'px') );

Note, the parseInt is redundant as the size() method will always return an integer.

Sign up to request clarification or add additional context in comments.

1 Comment

I used this instead and it worked great $('body').css('min-width', ((countColumns()) * 120) + 180 + 'px'); Thank you!
1

Since columns variable is defined inside the function it is not accessible outside it. You should define it in an outer or global scope. Try this.

var columns;
function countColumns() {
    columns =  $('.column').size(); 
    document.write(columns)
}

$('body').css('min-width', ((parseInt(columns) * 120) + 180) + 'px') );

Alternatively you can even call the function which will return the number of columns. In this way you don't have to maintain the variable. Also in countColumns function I am using lenght property of the jQuery object which gives the same result and also avoids one function call. Try this

   function countColumns() {
        return  $('.column').length; 
    }

    $('body').css('min-width', ((countColumns * 120) + 180) + 'px') );

3 Comments

You also have to call the countColumns() function... on the other hand, there's no reason to use the global variable if the function would just return the value.
Also, make sure you´re running the script once the DOM has loaded.
As a side note: consider LESS for things like this, it's wonderful.
0

you're setting the column variable from within the scope of the function, try this instead

var columns
function countColumns() {
    columns =  $('.column').size(); 
    document.write(columns)
}

$('body').css('min-width', (parseInt(columns) * 120) + 180 + 'px') );

Comments

0

This should work

$(document).ready(function() {

  var minWidth = ($('.column').length * 120) + 180);
  $('body').css('min-width', (minWidth + 'px') );

});

Never seen anyone use min-width on <body> though :P

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.