views:

81

answers:

1

Is there a simple and clean jQUery-way to find the element who a element is having it's relative position to? In other words: finding the first parent who has the css attribute position: relative, or the body-element (if no parent elements has position: relative).

I do -not- know the css-class or whatsover of the parent.

+2  A: 

Starting with element loaded up in $elem

var $closestRelativeParent = $elem.parents().filter(function() { 
  // reduce to only relative position or "body" elements
  var $this = $(this);
  return $this.is('body') || $this.css('position') == 'relative';
}).slice(0,1); // grab only the "first"
gnarf