tags:

views:

71

answers:

2

I try to make onpage searches like in modern browsers with STRG + F. I tried:

$("#mydiv").find(':contains(\'mySearchString\')').prepend('found you!');

The problem is that jQuery adds found you multiple times, bedause there are multipile elements who has the string. Example:

Found you<div>
   Found you<ul>
      Found you<li>
         Found you<a>mySearchString</a>
      </li>
   </ul>
</div>
+1  A: 

You could do:

$("#mydiv").find(':contains(\'mySearchString\')').eq(0).prepend('found you!');

to single out the first match.

Otherwise you could just say it works like Google Chrome, which highlights all search matches immediately ;)

David Hedlund
Firefox can do too :PBut so I can only show one of the matches.. and I would only mark the wrong element.. I need the last element, so in the example the element <a> and not <div>
gnol
+1  A: 
$('#mydiv').find(':contains(\'mySearchString\')').contents().filter(function(){return this.nodeType == Node.TEXT_NODE}).prepend('found you!');

would only select the last inner text node. If you are using IE, use the constant 3 instead of Node.TEXT_NODE.

Dave.Sol