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

  • SEARCH
  • Home
  • 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 6714357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:29:12+00:00 2026-05-26T08:29:12+00:00

How do I get the textrange to do a search for a div (or

  • 0

How do I get the textrange to do a search for a div (or a form)? Are there scripts already available, or jquery functions that search the text of a div?

I append a form to a div with this code:

    $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');

I’d like to have a search function that will only look for a string in that div. I specify the text range like this.

txt = window.document.body.getelementbyid(card_id).createTextRange();

The search function is one that I found on the net and that I am trying to update to search the div instead of the entire page. There will be several divs on the page and I want the search to be specific to each. I call that function from search_links(card_id);.

function search_links (card_id, form) {
    var search_str = document.frm_search.linkcard_search_string.value;
    /* alert('search_links '+search_str); */
    return search_linkcard(search_str, card_id);
}

var IE4 = (document.all);
var n   = 0;
function search_linkcard(str, card_id) {

  alert (card_id + ' ' + str);
  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (IE4) {
    txt = window.document.body.getelementbyid(card_id).createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        search_linkcard(str, card_id);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  }

  return false;
}

My specific questions are those at the beginning of the question: How do I specify a text range for the div? Is the syntax I have right? Are there scripts that already do what I want, i.e. search the contents of a specific div?

  • 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-05-26T08:29:13+00:00Added an answer on May 26, 2026 at 8:29 am

    Did the search with :contains. Did not do one match at a time. Highlighted all matches.

    // Open search
    function open_search(card_id) {
    
        $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form" onclick="clear_search(\''+card_id+'\', this.form);"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');
    
        var frm_elements = frm_search_link.elements;
        for(i=0; i<frm_elements.length; i++) {
            field_type = frm_elements[i].type.toLowerCase();
            switch (field_type)
            {
                case "text":
                    frm_elements[i].value = ""; 
                    break;
    
                default:
                    break;
            }
        }
    
    }
    
    // Close search
    function close_search(card_id, form) {
        $('form.frm_search_links', $('#'+card_id)).remove();
        var card_select = '#'+card_id;
        $('.link',$(card_select)).removeClass('search_results');        
    }
    
    // Search links
    function search_links (card_id, form) {
        var search_str = document.frm_search.linkcard_search_string.value;
        var search_select = '.link:contains('+search_str+')';
        var card_select = '#'+card_id;
        var result = $(search_select,$(card_select)).addClass('search_results');
        if (result.length == 0 || result.length == null) document.frm_search.linkcard_search_string.value = 'Not found.';
    }
    
    // Clear search
    function clear_search (card_id, form) {
        document.frm_search.linkcard_search_string.value = '';
        var card_select = '#'+card_id;
        $('.link',$(card_select)).removeClass('search_results');        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error: Can't locate Foo.pm in @INC Is there an easier way
.get() converts a jQuery object to a DOM element that Javascript can use without
I found this macro for Word that replaces text anywhere (headers, footers, body, text
I use something like this to get the text from a shape inside PowerPoint:
get current array position pointer while doing foreach $object->result_array() Hi, consider this, a case.
I get an error message from Firefox Unresponsive script. This error is due to
I get this error from my script: [Fri Apr 23 10:57:42 2010] [error] [client
Cant get this to work can any one help. List the part number, part
I'm trying to learn JQuery and I stumble upon this script in one of
Get data week-wise my mysql since jan-2011. Timestamp is unix time stamp.so that i

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.