I wrote a method that will provide a list of ancestors for any number of
generations until an ancestor matches the expression. Example:

BODY
 DIV
   UL.myClass
     LI
       UL
         LI #myId

$('#myId').ancestors()
returns [UL, LI, UL.myClass, DIV, BODY]

$('#myId').ancestors("UL.myClass")
returns [UL.myClass]

Returns all elements up to and including the match expression
$('#myId').ancestorsTo("UL.myClass")
returns [UL, LI, UL.myClass]

Code:
jQuery.fn.ancestorsTo = function(match) {
       var j = $();

       var b = false;
       $(this[0]).ancestors().each(function() {
           if (b == false) {
               j.add(this);

               if ($(this).is(match)) {
                   b = true;
               }
           }
       });

       return j;
   };


Cheers,
-js
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to