Symbios Symbios - 5 months ago 18
Javascript Question

Uncaught TypeError: cannot set property 'top' of undefined

I have this
error when I run my script. (The reason I have so many errors is because I put it in a timer)
My code is:

var ep; // create variable
var ev; // create variable

ep.top = $(charct).offset().top; // get top offset
ev.left = $(charct).offset().left; // get left offset

// charct is a div; var charct = document.getElementById('character');


I have an idea of what is causing this, (ep and ev are null variables) however, I still do not know how to fix this. Thanks in advance!

Answer

One can not define properties of undefined, create an empty object {} and then assign properties over it.

var ep = {}; // create variable
var ev = {}; // create variable

ep.top = $(charct).offset().top;  // get top offset
ev.left = $(charct).offset().left; // get left offset