0

i'd like to use these variables globally. the resize function does not work. only if i copy the variables. i want to use this code inside a document ready function.

thanks for your help.

    var pageWidth       = $('#page').width(),
        pageWidthFifth  = pageWidth / 5,
        wrapAndSidebar  = $('#sidebar-wrap, #sidebar');

    wrapAndSidebar.width(pageWidthFifth);


    $(window).resize(function(){

        wrapAndSidebar.width(pageWidthFifth);

    });
2
  • It's difficult to say without seeing the HTML markup for this, but I assume that you want to change the width of the sidebar based on the width of the #page element? If that's the case, you must define those variables in a local content, for example inside a function. At the moment they are being populated upon page load and remain the same all the time. Commented Feb 26, 2015 at 22:24
  • yes i wanna do this. just wondering how could i solve the problem not copy and pasting the variables again inside the resize function. Commented Feb 26, 2015 at 23:01

1 Answer 1

2

Just wrap everything in a function:

function resizeSidebar() {

    var pageWidth   = $('#page').width(),
    pageWidthFifth  = pageWidth / 5,
    wrapAndSidebar  = $('#sidebar-wrap, #sidebar');

    wrapAndSidebar.width(pageWidthFifth);

}

resizeSidebar();

$( window ).resize( function() {

    resizeSidebar();

} );

Please note I haven't tested this code in the browser, but it's pretty much what you must do. The idea is that all the variables are being re-populated on screen resize with their current values rather than the original ones.

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

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.