Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:45:39+00:00 2026-06-11T17:45:39+00:00

When I try to pass text which spreads throughout a few block elements the

  • 0

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.
The end result should be a GUI selection on the text in the page, I do not want to search if it exists.

I would like to know how to achieve this in Firefox(at least) and internet explorer.
Note: I can’t change the dom (e.g. change to display inline).

Edit:
Here is something I tried after @Alexey Lebedev’s comment, but it also finds the script (tag […] text):

Can I make it more simple? (better)?

function nativeTreeWalker(startNode) {
    var walker = document.createTreeWalker(
        startNode, 
        NodeFilter.SHOW_TEXT, 
        null, 
        false
    );
    var node;
    var textNodesV = [];
    var textNodes = [];
    node = walker.nextNode();

    while(node ) {
      if(node.nodeValue.trim()){
        textNodes.push(node);
        textNodesV.push(node.nodeValue);
        //console.log(node.nodeValue);
      }
        node = walker.nextNode();
    }
  return [textNodes,textNodesV];
}

var result = nativeTreeWalker(document.body);
var textNodes = result[0];
var textNodesV = result[1];

var param = " Praragraph.Test 3 Praragr";
paramArr = param.split(/(?=[\S])(?!\s)(?=[\W])(?=[\S])/g);
//Fix split PARAM
for(i=0;i<paramArr.length-1;i++){
  paramArr[i]= paramArr[i]+paramArr[i+1].charAt(0);
  paramArr[i+1] = paramArr[i+1].substring(1,paramArr[i+1].length);
}
//Fix last element PARAM
if(paramArr[paramArr.length-1] === ""){
  paramArr.splice(paramArr.length-1,1);
}
//console.log(paramArr);
var startNode,startOffset,sFound=false,
    endNode,endOffset;
for(i=0;i<paramArr.length;i++){
  for(j=0;j<textNodesV.length;j++){
    //Fully Equal
    var pos = textNodesV[j].indexOf(paramArr[i]);
    if(pos != -1){
      if(!sFound){
        startNode = textNodes[j];
        startOffset = pos;
        sFound=true;
      }else{
        endNode = textNodes[j];
        endOffset = pos+paramArr[i].length;
        break;
      }
    }
  }
}
console.log(startNode);
console.log(startOffset);
console.log(endNode);
console.log(endOffset);

var range = document.createRange();
range.setStart(startNode,startOffset);
range.setEnd(endNode,endOffset);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

Note: No jQuery (only Raw JS).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:45:40+00:00Added an answer on June 11, 2026 at 5:45 pm

    JS Bin demo: http://jsbin.com/aqiciv/1/

    If you want this to work in IE < 9 you’ll need to add MS-specific selection code (nightmare), or use Rangy.js (pretty heavy).

    function visibleTextNodes() {
        var walker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_ALL,
            function(node) {
                if (node.nodeType == 3) {
                    return NodeFilter.FILTER_ACCEPT;
                } else if (node.offsetWidth && node.offsetHeight && node.style.visibility != 'hidden') {
                    return NodeFilter.FILTER_SKIP;
                } else {
                    return NodeFilter.FILTER_REJECT;
                }
            },
            false
        );
    
        for (var nodes = []; walker.nextNode();) {
            nodes.push(walker.currentNode);
        }
        return nodes;
    }
    
    // Find the first match, select and scroll to it.
    // Case- and whitespace- insensitive.
    // For better scrolling to selection see https://gist.github.com/3744577
    function highlight(needle) {
        needle = needle.replace(/\s/g, '').toLowerCase();
    
        var textNodes = visibleTextNodes();
    
        for (var i = 0, texts = []; i < textNodes.length; i++) {
            texts.push(textNodes[i].nodeValue.replace(/\s/g, '').toLowerCase());
        }
    
        var matchStart = texts.join('').indexOf(needle);
        if (matchStart < 0) {
            return false;
        }
    
        var nodeAndOffsetAtPosition = function(position) {
            for (var i = 0, consumed = 0; consumed + texts[i].length < position; i++) {
                consumed += texts[i].length;
            }
            var whitespacePrefix = textNodes[i].nodeValue.match(/^\s*/)[0];
            return [textNodes[i], position - consumed + whitespacePrefix.length];
        };
    
        var range = document.createRange();
        range.setStart.apply(range, nodeAndOffsetAtPosition(matchStart));
        range.setEnd.apply(  range, nodeAndOffsetAtPosition(matchStart + needle.length));
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
        range.startContainer.parentNode.scrollIntoView();
    }
    
    highlight('hello world');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to pass text which spreads throughout a few block elements the
With window.open method I open new site with parameters, which I have to pass
I try to parse an xml file. The text which is in tags is
I have the following code, with which i try to dynamically pass my arguments
I have a helper method that serialises an object, which works until you try
I have a web from that has a few text boxes which are visible
What is the difference between ',' and 'as' in except statements, eg: try: pass
I try to pass variable from jquery to code c# but something is wrong.
I try to pass a structure as a parameter. Global structure: struct ThreadParams {
When I try to pass the results of a Linq query in to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.