5

I've looked at many questions regarding this subject and can not seem to find out what is wrong with my code. Any help would be greatly appreciated!

$(window).resize(function(){
   var newwidth = $(window).innerWidth();
   var newheight = $(window).innerHeight();      
   $("#element").height(newheight).width(newwidth);
       });

I'm trying to resize #element to the same height and width as the window if the window is resized.

2

5 Answers 5

13

About .innerWidth(), from docs:

This method is not applicable to window and document objects; for these, use .width() instead.

There is same note for .innerHeight() also.

So you need to use .width() and .height():

$(window).resize(function(){
    var newwidth = $(window).width();
    var newheight = $(window).height();      
    $("#element").height(newheight).width(newwidth);
});
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the response. It's interesting because if I alert(newwidth) i do get the correct width.
@Tyrsius Maybe there is a cross-browser compatibility problem.
@Engineer that's possible. I am using Chrome.
@MinaGabriel If inner(Width|Height) works on Chrome, it does not mean,it will work on all browsers.
1

try this:

$(window).resize(function(){
   var newwidth = $(window).width();
   var newheight = $(window).height();      
   $("#element").css({"height": newheight, "width": newwidth });
});

Comments

1

In plain Javascript you can do this:

    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
         viewportwidth = document.documentElement.clientWidth,
         viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
         viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
         viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    var mydiv = document.getElementById("element");
    mydiv.style.height=viewportheight'px';
    mydiv.style.width=viewportwidth+'px';

Comments

1

jsFiddle

 $(window).resize(function(){
var newwidth = $(window).width();
var newheight = $(window).height();      
$("#element").height(newheight).width(newwidth);
  });​

both works for me

update

 $(window).resize(function(){
var newwidth = $(window).innerWidth();
var newheight = $(window).innerHeight();      
$("#element").height(newheight).width(newwidth);
  });​

Comments

0

You can always just use CSS:

#element {width:100%; height:100%; position:fixed; }

1 Comment

downvote? this does the same thing the Op has in their code, except without the jQuery.

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.