My first jQuery plugin--"this" is not being set or
Posted: Tue Jul 03, 2007 2:25 pm
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.
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. 
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();