I'm trying to write a jQuery plugin. I've never done this before... what I'm trying to do is set up two methods, one called left() and one called top(). Basically, they would tell me how far from the left and top an element is respectively. I'm having difficulties. For some reason
this is not being set correctly, or I am misunderstanding something.
Code: Select all
jQuery.fn.extend({
left: function() {
alert(this.offsetParent); // alerts "undefined"
var curleft = 0;
if (this.offsetParent) {
curleft = this.offsetLeft
while (obj = this.offsetParent) {
curleft += obj.offsetLeft
}
}
return curleft;
}
$('#element').left();
Shouldn't this work? How come
this does not have an offsetParent attribute? When I look at #element in firebug, it's parent element is body, and it is most definitely set.
