1

I face a situation where google chrome returns

Uncaught TypeError: Cannot read property 'top' of undefined

This is my jquery code

$(document).on('click', '.show_popup', function(){ document.body.style.overflow = "hidden"; $('body').css('overflow', 'hidden'); callAjax($(this).attr('href'), 'mode=ajax', function(t){ $('.popup-screen').html(t); $('.main-overlay').show(); // $('').raty(); $('#star').raty({ click: function(score, evt) { $('#review_rating').val( score); } }); // window.scroll(0,0); $("html, body").animate({ scrollTop: $(".screenTable").offset().top },500); }); return false; }); 

Please help and also after error at

XMLHttpRequest.self.xmlHttpReq.onreadystatechange

4
  • What about the .screenTable element(s)? Can you ensure that at least one matching element exists on your HTML when this code is run? Commented Jul 27, 2017 at 5:31
  • Go into the debugger and find out what the value of $(".screenTable").offset() is Commented Jul 27, 2017 at 5:31
  • I think jQuery not get the element. add your html code. Commented Jul 27, 2017 at 5:55
  • please add your html code,so that people can check Commented Jul 27, 2017 at 5:58

1 Answer 1

2

This error is showing up because element with class .screenTable could not be found.

You can check the existence of element before calling top to prevent error

 var STable = $('.screenTable'); if (STable.length) //1 { $("html, body").animate({ scrollTop: $(".screenTable").offset().top },500); alert("top"); } else{ alert("element not found, so no top"); } 

Always wrap your function under $( document ).ready() More info on document.ready

$( document ).ready(function() { //All elements has been loaded and are available. }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.