So I have something like this:
<div class="main">
<div class="useless">
text I don't want.
</div>
<div class="useless">
text I don't want.
</div>
<div class="narrow">
text I'm searching for
</div>
<div class="useless">
text I don't want.
</div>
<div class="useless">
text I don't want.
</div>
</div>
Using jQuery's lovely :contains
selector, I can search for a keyword, but it will return both the parent and the child.
I'd like it to only return elements who directly contain the word for which I'm searching.
Stackoverflow usefully suggested this link as a previous attempt, but it seems extremely clunky since it's crawling all dom nodes and requires a lot of unclear code.
The solution, which is provided in the other question as referenced above but as a not highly regarded solution apparently, is to do this:
I'd add it's less clunky than writing a huge thing to crawl all DOM nodes, and really should affect less places when narrowly scoped.
If anyone else has better ideas, I'm definitely interested.
http://jsfiddle.net/TT7dR/42/
Try This In JS
This script will find all nodes that contain a specific text. It will also allow you to do any string tests on the text (regex etc).
Here is a fiddle for testing: http://jsfiddle.net/85qEh/4/
PS - For increased speed use a different top level selector. For example if you only need inside #container then you would
var textNodes = $('#container')...