phpphpquery

closest method in phpQuery?


I'm trying to see if there's a way to implement
the "closest" method on phpQuery (just like it works on jQuery).
Is there such a thing?


Solution

  • From Commonly Confused Bits Of jQuery:

    CLOSEST(SELECTOR)

    This is a bit of a well-kept secret, but very useful. It works like parents(), except that it returns only one parent/ancestor. In my experience, you’ll normally want to check for the existence of one particular element in an element’s ancestry, not a whole bunch of them, so I tend to use this more than parents().

    So as parents() exists in phpQuery you can go with the example from the source

    Tip: you can simulate closest() by using parents() and limiting it to one returned element.

    $($('#element1').parents('#element2').get(0)).css('background', '#f90');