jesper
jesper

Reputation: 889

Find text spanning multiple nodes

I'm searching a way to find text spanning multiple nodes in similar way Firefox does it, eg.

With given HTML:

<p>Lorem ipsum <b>dolor</b> sit amet.</p>

When I search for text "ipsum dolor" by ctrl+f Firefox will selects that text, ie. will create Range object(s).

I know I can easily search for text within text nodes (vide Find text string using jQuery?) but this doesn't work in above example.

Upvotes: 0

Views: 326

Answers (2)

jesper
jesper

Reputation: 889

window.find is exactly what I'm looking for.

Upvotes: 1

James Allardice
James Allardice

Reputation: 166021

This will select all p elements that contain the text specified as an argument to indexOf. The text method gets the contents of all text nodes of an element, so the b tag in your example will not matter:

$("p").filter(function() {
  return $(this).text().indexOf("ipsum dolor") > -1;  
});

See it working here.

Upvotes: 1

Related Questions