i can’t get this through.
I need to get offset().top from a jquery object, this is my code
parentLi = $(element).parents("li");
parentLiOffset = parentLi.offset();
top = parentLiOffset.top;
left = parentLiOffset.left;
console.log(parentLiOffset);
console.log(top);
console.log(left);
And this is what console gives back:
Object { top=208, left=311}
Window /demo/
311
as you can see, I can’t manage to get the “top” value. I’m usinf firefox, if that makes any difference.
Thanks!
The problem is that you haven’t used the
varkeyword to declare your variables.topis a property ofwindow(a read-only property, which is why your code doesn’t overwrite it). Do this instead:leftworks withoutvar, becauseleftis not a property ofwindow. Your code creates a property ofwindownamedleftand assigns the correct value to it. However, it is good practice to always use thevarkeyword to prevent variables leaking into the global scope.The
topproperty “returns a reference to the topmost window in the window hierarchy”.