Search code examples
javascripthtmltwitter-bootstrap-3bootstrap-typeahead

Twitter Typeahead - How I can use "afterSelect" to jump to annother section of the website?


I tried to jump after selection with "window.location.hash = '#section3';", but it doesn't work this way..

$('#inputField').typeahead({
        source:  function (query, process) {
            return $.get('test.php', { query: query }, function (data) {
                console.log(data);
                data = $.parseJSON(data);
                return process(data);
            });
        },
        afterSelect: function (item) {
            window.location.hash = '#section3';
        }
    });

Thanks for your help!


Solution

  • Change the window.location to scrollTop

    SCRIPT

    $('#inputField').typeahead({
        source:  function (query, process) {
            return $.get('test.php', { query: query }, function (data) {
                console.log(data);
                data = $.parseJSON(data);
                return process(data);
            });
        },
        afterSelect: function (item) {
            //window.location.hash = '#section3';
            $("html, body").animate({ scrollTop: $('#section3').offset().top }, 1000);
        }
    });