You could do this with the :has() CSS psuedo-class[0], though inverted (select a parent that _has_ the child matching a selector).
Looks like that psuedo-class has not been implemented in the kuchiki library that htmlq uses though.
[0]: https://developer.mozilla.org/en-US/docs/Web/CSS/:has