JQuery - Cannot read property top of undefined

I've been receiving this error whenever try running this piece of Jquery code:

$(document).ready(function(){
  $("#Card-Right-Sec").style.top = $("#Card-Left-Sec").style.top;
}); 

Uncaught TypeError: Cannot read property 'top' of undefined at :1:59

I'm basically trying to align two elements with each other no matter how much the screen resizes but that has not been working out too well for me...

I'm ready to take Any suggestions at this point.

Answers 1

  • There is no style property for jQuery Object, use css() method on jQuery object.

    $(document).ready(function(){
      $("#Card-Right-Sec").css('top', $("#Card-Left-Sec").css('top'));
    }); 
    

    Or get DOM object by index or using get() method and update the style property.

    $(document).ready(function(){
      $("#Card-Right-Sec")[0].style.top = $("#Card-Left-Sec")[0].style.top;
    }); 
    

Related Articles