How to find the next element above an inner element with class=bbb?

How to find the next element above an inner element with class=bbb?


Assume we have the following web page source code

  1. ...
    <p>
    ....
    <p ....>
    <span class="aaa">
    <a class="bbb">....</a>
    ...sampletext
    </span>
    </p>
    </p>

How can I address/refer only (!) to the next <p> element above an inner leement which is defined by class=bbb?

If one can refer to a simple string like "sampletext" then one could write successfully:

  1. $("p:contains('sampletext')").eq(($("p:contains('sampletext')").length) - 1).css("display", "none");

But what if I do not have identifying string like "sampletext" but only an element with unique class=bbb?

The following does not work:

  1. $("p:contains(.bbb)").eq(($("p:contains(.bbb)").length) - 1).css("display", "none");

How do I have to rewrite this statement?

Does your solution work with unique IDs as well?

Peter