Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

offset() is undefined 'error'

While loading a page, I want to get the top of the panel. But it is showing errors. In Firefox and IE its saying "offset() is undefined" and in Chrome the error is "Uncaught TypeError: Cannot read property 'top' of undefined". Please Help out.

$(window).resize(function () {
    tabPanelAdjust();
});

function tabPanelAdjust() {
    var theTabPanel = $(".TabPanel");
    var tabPanelTop = parseInt(theTabPanel.offset().top);
    var winHeight = $(document).height();
    tabPanelHeight = winHeight - tabPanelTop;
    theTabPanel.css({ "min-height": tabPanelHeight - 20 });
}

<div class="TabPanel">
</div>
like image 617
Arpit Gupta Avatar asked Oct 18 '25 17:10

Arpit Gupta


1 Answers

There's nothing wrong with the code. include your code within document ready method. May be your code is fired before the div is loaded.

$(function(){
$(window).resize(function () {
  tabPanelAdjust();
});

function tabPanelAdjust() {
 var theTabPanel = $(".TabPanel");
 var tabPanelTop = parseInt(theTabPanel.offset().top);
 var winHeight = $(document).height();
 tabPanelHeight = winHeight - tabPanelTop;
 theTabPanel.css({ "min-height": tabPanelHeight - 20 });
}
});

Here's a working fiddle:

http://jsfiddle.net/SX5U4/

like image 97
Vivek S Avatar answered Oct 20 '25 06:10

Vivek S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!