views:

230

answers:

4

Hi Guys,

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

+5  A: 

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

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

// for the window resize
$(window).resize(function() {
    var bodyheight = $(document).height();
    $("#sidebar").height(bodyheight);
});
Sarfraz
A: 

Use the resize event.

This should work:

 $(document).ready(function() {
   $(window).resize(function() {
    var bodyheight = $(document).height();
    $("#sidebar").height(bodyheight);
}); });
Pekka
You don't need the resize event handler within your $(document).ready
Ken Ray
+1  A: 

In addition to what others have said make sure you extract the event handler code into a separate function and call it from ready and resize event handlers. That way if you add some more code or need to bind it to other events you will have only one place where to change code logic.

Giorgi
What you should also do in that case is make sure you set the height in the correct place. Don't make a mistake I once did, and set the height of the two div's that comprised my page, then set the width of one of the columns - which changed the height, resulting in an overflow situation.
Ken Ray
A: 

this is not working..

kalki