When I try to pass text which spreads throughout a few block elements the window.find method dosent work:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
</head>
<body>
<p>search me</p><b> I could be the answer</b>
</body>
</html>
JavaScript:
window.find("meI could be");
Or:
str = "me";
str+= "\n";
str+="I could be t";
window.find(str);
This happens when the <p> element is present between the search term.
How can I fix that?
As option:
Just tested it and it’s working same as window.find. Or window.find is working same as this my function.
Anyway seems it’s depends to element’s style.display property.
E.g. when I set
this call
window.find("me I could be")returnstruefor your HTML example.I created this example to test mentioned behavior.
As option you can create a
divelement in memory, get document.body.innerHTML and set retrieved value to div’sinnerHTML, then changestyle.dysplayto"inline"for elements within thisdivsimilar to what I did in my code example, and then perform a search.UPDATE #1: jQuery
I’ve made deeper research and found that usage of jQuery
:containsselector is working better than my previous function and than window.find, but may be more expensive (not sure, need to be tested).UPDATE #2: pure JavaScript solution