I have a div with an id "main", I want to set its height using jQuery and making so that on window.resize
its height is modified using a function but for some reason it works well only when increasing window's height, when decreasing it it doesn't work.
This is the JS:
function setMainH() {
var docH = $(document).height();
$("#main").height(docH - 40);
}
$(function() {
setMainH();
});
$(window).resize(function() {
setMainH();
});
EDIT: the problem appears either increasing or decreasing
EDIT 2: the resize()
event seem to be called correctly, I've tried with
console.log("resizing");
on resizing and it logs correctly so the problem isn't that.
So changing
to
will solve the problem.
I tested your code and it works fine in jsfiddle when running the JS without a wrapper in the body or head.
Demo of your code: http://jsfiddle.net/H5y5e/2/
You can however optimize your code a bit to clear-up the global space and cache the
$(document)
and$('#main')
selectors so they doesn't have to be looked-up eachresize
event (which is a ton of events when the window resizes):Here is a demo: http://jsfiddle.net/H5y5e/1/
UPDATE
Also it seems like this can easily be done with just CSS (which is always good, takes care of the edge-case where someone has JS turned off):
Demo using just CSS: http://jsfiddle.net/H5y5e/3/