user2142786
user2142786

Reputation: 1482

error: Cannot read property 'top' of null

i want to get the top of my footer.but i am getting this error: Cannot read property 'top' of null

here is my html

<footer class="footer" role="complementary" id="myfooter">

</footer>

and in jquery

var topoffooter=$('#myfooter').offset().top;

can anybody help regarding this

Upvotes: 0

Views: 2152

Answers (1)

Rohit Agrawal
Rohit Agrawal

Reputation: 5490

PROBLEM

The problem might be happening because your script is not able to find the DOM element. The element should be there in the DOM before it is used by javaScript


DEBUGGING

Instead of this line var topoffooter=$('#myfooter').offset().top; just try to alert the length of jQuery object

alert($('#myfooter').length);

It must be greater than zero to work, and if not we are on the right path to solve the issue.


SOLUTION

1) You can put the scripts just before the closing of body tag - </body> - PREFERRED

2) Or, execute your scripts on document(DOM) ready event using jQuery's
$(document).ready() http://api.jquery.com/ready/

Upvotes: 3

Related Questions