The following should work, but it is not the most efficient solution though:
$("body, body *").contents().filter(function () {
return this.nodeType === 3 && this.nodeValue.indexOf("Foo & Bar") >= 0;
}).parent(":visible");
This code:
- Locates all text nodes in the document that contain the specified string 1
- And checks if their parent is visible 2
1 This means the deepest node that contains the string. The :contains
selector would be useless because it could return a visible element (in fact a branch of elements) that contains an invisible child that contains the specified string.
2 An element is visible if its parents are visible as well