1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

Discussion in 'jQuery' started by matheusgrafix, Jun 30, 2016.

  1. #1
    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);
                    }
                }
    Code (JavaScript):

    This one is responsible for using the Enter key to trigger the previous function:
    if(e.which==13){ // 13 = enter key
                e.preventDefault();
                arrowOffset++;
            }
    Code (JavaScript):
    Thx in advance.
     
    matheusgrafix, Jun 30, 2016 IP