3 Replies - 751 Views - Last Post: 05 July 2016 - 09:09 AM

#1 spikespiegel007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 16

jQuery live search - Go back to first result after the page has found

Posted 30 June 2016 - 03:41 PM

I'm using mark.js plugin that allows to search text on page, it finds text while you type and if you hit the Enter key, you'll jump to the next result until it has found and highlighted all results.
The only problem is that when it reaches the last result, it gives the search as concluded and the search can't be restarted from the first result if you hit Enter.

I need to be able to scroll back to the first result after the last result has been found (Just like it happens when you're searching something using Google Chrome Find Box (Control + F).

[Working Fiddle] https://jsfiddle.net/70okvfr1/6/

You need to scroll the result tab in the fiddle a little bit, in order to see the Search Box.

The following function is responsible for scrolling between the results, it will jump to the last (Next) found text on page:

 done: function() {
                var mark = $('mark[data-markjs]').last();    // Scroll to last <mark>
                if (mark.length) {
                    $('html,body').animate({scrollTop: mark.offset().top-100}, 100);
                }
            }


This one is responsible for using the Enter key to trigger the previous function:

if(e.which==13){ // 13 = enter key
            e.preventDefault();
            arrowOffset++;
        }


thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: jQuery live search - Go back to first result after the page has found

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: jQuery live search - Go back to first result after the page has found

Posted 30 June 2016 - 03:51 PM

Part of the handler for the enter key should check if it is on the last match and, if so, set the counter back to 1.
Was This Post Helpful? 0
  • +
  • -

#3 spikespiegel007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 16

Re: jQuery live search - Go back to first result after the page has found

Posted 01 July 2016 - 05:53 PM

View PostArtificialSoldier, on 30 June 2016 - 03:51 PM, said:

Part of the handler for the enter key should check if it is on the last match and, if so, set the counter back to 1.


This is the solution I've got, but now the page won't automatically scroll when I type in the search box:
https://jsfiddle.net...iford/p3026h8s/
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: jQuery live search - Go back to first result after the page has found

Posted 05 July 2016 - 09:09 AM

Debugging on jsfiddle is a pain in the ass, but I would suggest adding some breakpoints to your event handler to see what happens when you press a key.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1