Page 1 of 1

My first jQuery plugin--"this" is not being set or

Posted: Tue Jul 03, 2007 2:25 pm
by Luke
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. :(

Posted: Tue Jul 03, 2007 3:43 pm
by superdezign
That's strange. It's got to be jQuery because in FF, offsetParent is the highest element no matter what.

Maybe you can't assign the function to the object....? I'm not sure how that is done without the adding an event listener.


Try this and see if you get the same error:

Code: Select all

alert(document.getElementById('element').offsetParent);