How to change height div on window resize?

user278139 picture user278139 · May 3, 2010 · Viewed 93.1k times · Source

I have a div on my website that should be the height of the window. This is what i got:

    $(document).ready(function() {
    var bodyheight = $(document).height();
    $("#sidebar").height(bodyheight);
});

However, it does not automatically change when the window is resized? Does any body know how to fix this?

Thanks

Answer

Sarfraz picture Sarfraz · May 3, 2010

You also need to do that in resize event, try this:

$(document).ready(function() {
    $(window).resize(function() {
        var bodyheight = $(this).height();
        $("#sidebar").height(bodyheight);
    }).resize();
});