0

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.

Community
  • 1
  • 1
jesper
  • 879
  • 8
  • 21
  • so you mean to ignore the tags? – ianace Aug 12 '11 at 10:52
  • In a manner of speaking - yes, but I also must have ability to create Range (ie. select that text, emphasize it somehow). Sorry if I haven't stand it clear. – jesper Aug 12 '11 at 10:57

2 Answers2

1

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.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

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

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
jesper
  • 879
  • 8
  • 21
  • Discussion over at whatwg.org suggests it's not in Opera :-( http://developers.whatwg.org/dnd.html#text-search-apis – eimaj Aug 12 '11 at 11:38
  • Yes :(. This works in FF, Chrome, Safari. In IE I can use TextRange object. But I can not find similar functionality in Opera :/ – jesper Aug 12 '11 at 11:58