Skip to content
Thomas Weinert edited this page Jul 12, 2018 · 2 revisions

FluentDOM\Query::closest()

FluentDOM\Query closest(string $selector [, array|Traversable $context = NULL]);

Get a set of elements containing the closest parent element that matches the specified selector, the starting element included.

Usage

$xml = <<<HTML
<html>
  <head>
    <title>Examples: FluentDOM\Query::closest()</title>
  </head>
  <body>
    <ul class="myList">
      <li class="red"><b>The <i>first <u>item</u></i></b>.</li>
      <li class="green"><b>The <i>second <u>item</u></i></b>.</li>
      <li class="yellow"><b>The <i>third <u>item</u></i></b>.</li>
      <li class="blue"><b>The <i>fourth <u>item</u></i></b>.</li>
    </ul>
    <p>
      Class of the last clicked item: <span id="display"> </span>
    </p>
  </body>
</html>
HTML;

$fdQuery = FluentDOM($xml, 'text/html');
echo $fdQuery
  ->find('//u')
  ->closest('self::li')
  ->addClass('foundIt');

Output

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>Examples: FluentDOM\Query::closest()</title></head><body>
    <ul class="myList"><li class="red foundIt"><b>The <i>first <u>item</u></i></b>.</li>
      <li class="green foundIt"><b>The <i>second <u>item</u></i></b>.</li>
      <li class="yellow foundIt"><b>The <i>third <u>item</u></i></b>.</li>
      <li class="blue foundIt"><b>The <i>fourth <u>item</u></i></b>.</li>
    </ul><p>
      Class of the last clicked item: <span id="display"> </span>
    </p>
  </body></html>
Clone this wiki locally