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 7497789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:13:37+00:00 2026-05-29T19:13:37+00:00

I need to be able to loop through the next occurrence of a given

  • 0

I need to be able to loop through the next occurrence of a given text on a page. Just like the most common ‘find’ function on almost every software (F3 – find next).

I’m trying to to that using jQuery but can’t make it work by any means. Tried: NextAll(), next(), closest() (which seems bugged), find(), eq(), children(), etc., etc., etc.

Below is a sample that do work but it goes to the last element on the page and do not loop through.

function scrollMe(tow){
    var targetOffset = $("*:contains('"+tow+"'):last").offset().top;
    $('html,body').animate({scrollTop: targetOffset}, 1000);
}

To make it clear my page has a set of rows(divs) with text inside. Every time a user click on this row it must gently roll down (or up) to the next row with the occurrence of the text (if any).

Sample:

<div onclick="scrollMe('hello');">hello</div>
<div onclick="scrollMe('world');">world</div>
<div onclick="scrollMe('foo');">foo</div>
<div onclick="scrollMe('hello');">hello</div>
<div onclick="scrollMe('bar');">bar</div>

Indeed it should be enclosed by jQuery but it’s just to illustrate.

  • 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-29T19:13:39+00:00Added an answer on May 29, 2026 at 7:13 pm

    Here is a working example of scrolling to the next occurrence and highlighting it.

    Since you’re going to use a variable as the input to contains, I’d recommend skipping the selector. It’s fast, but you’re going to have trouble keeping the variable input sanitized.

    This will, for example, highlight all the text occurrences of ‘two’ (fiddle example):

    jQuery(function($) {
       var findText = 'two';
        $('*').filter(function() {
            return $(this).children().length < 1 && $(this).text().indexOf(findText) >= 0;
        }).addClass('hilite'); 
    });
    

    To make this work with some sort of find next functionality, you would need a variable to keep track of the current index, and some sort of trigger:

    jQuery(function($) {
       // stores the currently highlighted occurence
       var index = 0;
       var findText = 'sed';
    
       // you could do this inside the click as well, here, it's cached/faster
       // inside click, it would be more dynamic and prevent memory leaks
       // just depends on your needs
       // you would also want to start with a parent element as $('*') is costly!
       var $occurrences = $('*').filter(function() {
           return $(this).children().length < 1 && $(this).text().indexOf(findText) >= 0;
        });
    
        // remove existing highlights, then find the next occurrence and highlight it
        $('#trigger').click(function() {
           if( index == $occurrences.length-1 ) { index = 0; }
           $occurrences.find('span.hilite').replaceWith(findText);
           var $next = $occurrences.eq(++index);
           $next.html( $next.html().replace(findText, '<span class="hilite">'+findText+'</span>') );
           $(document).scrollTop($next.offset().top-35);
           return false;
        });
    
        // scroll our trigger link when the screen moves so we can click it again
        $(window).scroll(function() {
            var top = $(window).scrollTop();
            $('#trigger').offset( {top: top, left: 0} );
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to loop through an HTML table and output data.
I need to be able to loop through column names and put the coloumn
I need to be able to get at the full URL of the page
I need to be able to load the entire contents of a text file
I need to use VBScript to loop through a set of registry string value
i have a combobox whose datasource is a datatable. i need to loop through
I have the need to be able to accurately find the months between two
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to create basic MS Project items (tasks, projects, resources,

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.