4

I'm attempting to apply the height of window to the max-height of a particular div.

$(document).ready(function(){
        console.log('Initial Window height:', $(window).height());
        $('.wordboard').css({ "max-height": ($(window).height()-150) + 'px' });
        //console.log($('.wordboard').css({"max-height": ($(window).height() - 150) + 'px'}));


        console.log("Initial max height of $('.wordboard'): ", $('.wordboard').css("max-height"));
        $(window).resize(function(){
            $('.wordboard').css({ "max-height": ($('body').height()-150) + 'px' });
            console.log("$('.wordboard').css('max-height')",$('.wordboard').css('max-height'));
        });

    });

However, the my website is loaded: the max-height isn't applied. The result in the console is when the website is loaded is:

Initial Window height: 550
script.js:28 Initial max height of $('.wordboard'):  undefined
(After i resize the window, the effect is now apply. However, I want to apply as soon as the window is loaded)

script.js:31 $('.wordboard').css('max-height') 718px
script.js:31 $('.wordboard').css('max-height') 723px
script.js:31 $('.wordboard').css('max-height') 728px
script.js:31 $('.wordboard').css('max-height') 733px
2
  • Might have something to do with the '???px' not being wrapped in quotes. Commented Jun 4, 2015 at 10:06
  • 2
    Can you provide jsFiddle replicating your issue? Commented Jun 4, 2015 at 10:11

3 Answers 3

4

The only solution comes in mind is that at time you call your snippet on document ready pseudo event, your element(s) .wordboard doesn't exist in the DOM. You are surely (or a plugin) adding it dynamically.

EDIT: just check it:

$(function(){console.log($(".wordboard").length);});

If displaying 0, you know from where comes from your issue.

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

2 Comments

Thanks. That's must be the problem.
Yeh. Turn out I just need to move the script to the bottom of the page
3

This is the correct way.

$('.wordboard').css('max-height',"723px");

3 Comments

Why this fixed number? Any particular reason?
this is also the correct. $('.wordboard').css({ "max-height": ($(window).height()-150) + 'px' });
Not sure how this address OP's issue?!
0

You have the code inside an event, the resize event.

It will only execute when that event is triggered. (As you verified)

Now, you have 2 options:

  1. Trigger the resize event:
    $(window).resize(function(){ [...] }).resize();
    Yes, just add .resize() after the event declaration. It's that simple!
  2. Duplicate the code:
    $(window).resize(function(){ [...] }); $('.wordboard').css({ "max-height": ($('body').height()-150) + 'px' });

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.