2

I have a div (id="mainDiv") which I need to dynamically resize if the user changes the size of their browser window. I have written the following code to try and get this to work however this doesn't seem to be setting the height of my div:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    var height = $(window).height(); 
    $("#mainDiv").height(height);
</script>
<body>
    <div id="mainDiv"></div>
</body>

I don't know much jQuery, am I making an obvious mistake?

3
  • This code does not run when the browser is resized. It runs immediately and never again. Commented Apr 8, 2013 at 20:49
  • 1
    you need to put your resize code in the resize function api.jquery.com/resize also make sure it is in doc ready Commented Apr 8, 2013 at 20:49
  • 4
    You could easily use CSS for this when using percentages because the div is the full window. Commented Apr 8, 2013 at 20:49

5 Answers 5

7

There are a few things going wrong here:

  1. Your code will execute straight away before your document has finished loading.
  2. Your code will execute only on load of the the script, not on resize
  3. You're not setting the height of your mainDiv - you're setting the height of another element with the id: accordianMain (but I'm guessing you know that).

You need to handle the browser resize event to wait for the browser to resize then get the dimensions of the window and apply accordingly. You'd do this by handling the window.resize event liks this:

var $win = $(window);

$win.on('resize',function(){
    $("#mainDiv").height($win.height());
});

What you probably want to do is wait for for a resize to finish to by adding a timeout so that it doesn't fire too often as well:

var timer,
    $win = $(window); 

$win.on('resize',function(){
    clearTimeout(timer);
    timer = setTimeout(function(){
        $("#mainDiv").height($win.height());
    }, 500);

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

Comments

2

You need to wrap it in a resize function:

$(window).on('resize',function(){
    var height = $(window).height(); 
    $("#mainDiv").height(height);
});

Although I should point out what your doing could easily be attained through CSS:

body,#mainDiv {
    height:100%;
}

This is more to point out the "it needs to be in a resize event wrapper" larger point, in case you wanted to do some logic that actually needed the event.

5 Comments

Whilst I agree that the CSS is a better solution, there is no guarantee that the height:100% will work as hoped with the CSS that is being used here as the body is probably not 1005 of the window height.
"the CSS that is being used here" ... there is no CSS here. With the HTML he provided, and lacking any other CSS or elements as the example does ... yes, it will work. simple examples in questions yield simple examples in solutions.
What makes you think that body will be 100% height of the window?
you'll need to set html, body, #mainDiv - all to be 100%
Right, I'm just trying to help! That will work in JS fiddle, but try that in a full browser where the body doesn't make the full height of the window. Its not a pissing match, don't take my word for it: stackoverflow.com/questions/14151663/…
1

Yes, the mistake is use jquery hehehe

You do this with CSS

<body>
    <div id="mainDiv" style="height:100%"></div>
</body>

Comments

0

Use an event listener for the window resize (and you should wait for page load before accessing elements with JQuery):

$(function() {
  $(window).on('resize', function() {
    var height = $(window).height(); 
    $("#mainDiv").height(height);
  });
});

Comments

0

I used div content and div footer in my page, i will set this code in my js to set dynamic height for div content.

 footer = $("div[data-role='footer']");
    content = $("div[data-role='content']");
    viewHeight = $(window).height();
    contentHeight = viewHeight - footer.outerHeight();
    contentHeight -= (content.outerHeight() - content.height());
    content.height(contentHeight);

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.