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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:46:04+00:00 2026-06-14T10:46:04+00:00

I’ve a search feature in one of code which ideally performs a search on

  • 0

I’ve a search feature in one of code which ideally performs a search on the list of values (small number).

<li>abc</li> 
<li>def</li> 
...

I’ve a search button in the bottom, I put a keyup event listener to get instant searches. Now the question is how can I make it navigable.
Ideal scenario search for some text, hit enter and it should open the first element, else you should be able to navigate through.

If I’m not clear above I’m actually asking for a feature very similar to Facebook Chat sidebar search.

  • 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-06-14T10:46:05+00:00Added an answer on June 14, 2026 at 10:46 am

    For the sake of the OP who has not added jQuery to the list of tags ( I congratulate you ), I’ll add a small navigation plugin in pure JavaScript.

    Disclosure – the credit for the actual logic would go to Eric Priestley of Facebook fame as this was lifted from his Javelin JS library.

    I’m posting a link to the fiddle here. I also assume from your question that you already have a search solution in place and are simply looking for a navigation solution.

    Cross posting from fiddle. I’ve commented a lot in the code, so should be self explanatory. Though if you do have doubts, get in touch.

    HTML
    Can be anything you desire. For brevity, I’ve added this

    <input id='control' type='text' placeholder='Search text here' />
    <!-- List of results - hardcoded for now -->
    <ul id='hardpoint' class='hidden'>
        <li class='item'>Search item 1</li>
        <li class='item'>Search item 2</li>
        <li class='item'>Search item 3</li>
        <li class='item'>Search item 4</li>
    </ul>​
    

    The code

    (function() {
    
    
        /**
         * Helper functions
         */
        var bind = function( context, func, more ){
            if (typeof func !== 'function') {
                throw new Error ("No function supplied to bind function");
            }
            var bound = Array.prototype.slice.call( arguments ).slice(2);
            if (func.bind) {
                return func.bind.apply(func, [context].concat(bound));
            }
            return function() {
                return func.apply(context || window, bound.concat(Array.prototype.slice.call( arguments )));
            }
        };
    
        var getChildElements = function(node) {
            var childNodes = Array.prototype.slice.call(node.childNodes);
            childNodes = childNodes.filter(function(item) {
                return item.tagName && item.nodeType && item.nodeType === 1 && item !== node;
            });
            return childNodes;
        };
    
        var hasClass = function(node, className) {
            return ((' ' + node.className + ' ').indexOf(' ' + className + ' ') > -1);
        };
    
        var alterClass = function(node, className, add) {
            var has = hasClass(node, className);
            if (add && !has) {
                node.className = node.className.trim() + ' ' + className;
            }
            else if (has && !add) {
                node.className = node.className.replace(
                new RegExp('(^|\\s)' + className + '(?:\\s|$)', 'g'), ' ');
            }
        };
    
        var getIndex = function( list, element ){
            var index = -1;
            for (var i = 0; i < list.length; i++) {
                if( list[i] === element ){
                    index = i;
                    break;
                }
            };
            return index;
        };
    
    
        // Start of plugin
        // Constructor
        var ListNavigator = function(config) {};
    
        // This can be moved within constructor if you so prefer
        ListNavigator.prototype.init = function(config) {
            // On what element do you want the controls to activate
            // Pressing up down etc on this element, triggers tha navigation
            this.control = document.getElementById(config.control);
    
            // The list of results that can be navigated
            this.hardpoint = document.getElementById(config.hardpoint);
    
            // A list of items ( usually, childNodes ) of hardpoint
            // Dynamically populated
            this.display =  getChildElements(this.hardpoint);;
    
            // What to set the focus on initially
            this.focus = -1;
    
            // Specify a function to execute when the user chooses a result
            // Keydown - Return : configured to be the choose event type now
            this.choose = config.choose;
    
            this.selector = config.selector;
        };
    
        ListNavigator.prototype.run = function() {
            var controlEvents = ['focus', 'blur', 'keydown', 'input'],
                mouseEvents = [ 'mouseover', 'mouseout', 'mousedown' ],
                self = this,
                selector = this.selector;
    
            controlEvents.forEach(function(event) {
                self.control.addEventListener(event, bind(self, self.handleEvent), false);
            });
    
            mouseEvents.forEach(function(event) {
                self.hardpoint.addEventListener(event, bind(self, self.onmouse), true);
            });
        };
    
        // Logic to change the focus on keydown
        ListNavigator.prototype.changeFocus = function(d) {
            var n = Math.min( Math.max( -1, this.focus + d ), this.display.length - 1);
    
            if (this.focus >= 0 && this.focus < this.display.length) {
                alterClass(this.display[this.focus], 'focused', false);
            }
    
            this.focus = n;
            this.drawFocus();
            return true;
        };
    
        // Set the focus on the targetted element
        ListNavigator.prototype.drawFocus = function() {
            var f = this.display[this.focus];
            if (f) {
                alterClass(f, 'focused', true);
            }
        };
    
        // Handle mouse events
        ListNavigator.prototype.onmouse = function(event) {
            var target = event.target, type = event.type;
            if ( hasClass( target, this.selector ) ) {
                if ( type === 'mousedown' ) {
                    // Choose this element
                    this.choose(target);
                }
                else if ( type === 'mouseover' ) {
                    // Set the focus to element on which mouse is hovering on
                    this.focus = getIndex( this.display, target );
                    this.drawFocus();
                } 
                else if ( type === 'mouseout' ){
                    // Reset the display to none
                    this.changeFocus(Number.NEGATIVE_INFINITY);
                }
            };
        };
    
        ListNavigator.prototype.handleEvent = function(e) {
            var type = e.type;
            if (type === 'blur') {
                this.focused = false;
                this.hide();
            } else {
                alterClass(this.hardpoint, 'hidden', false);
                this.update(e);
            }
        };
    
        ListNavigator.prototype.hide = function() {
            this.changeFocus(Number.NEGATIVE_INFINITY);
            this.display = [];
            alterClass(this.hardpoint, 'hidden', true);
        };
    
        ListNavigator.prototype.submit = function() {
            if (this.focus >= 0 && this.display[this.focus]) {
                this.choose(this.display[this.focus]);
            } else {
    
                if (this.display.length) {
                    this.changeFocus(1);
                    this.choose(this.display[this.focus]);
                }
            }
            return false;
        };
    
        ListNavigator.prototype.update = function(event) {
    
            console.log( 'upadte' );
            this.display = getChildElements(this.hardpoint);
    
            if (event.type === 'focus') {
                this.focused = true;
            }
    
            var k = event.which;
            if (k && event.type == 'keydown') {
                switch (k) {
                case 38:
                    if (this.display.length && this.changeFocus(-1)) {
                        event.stopPropagation();
                    }
                    break;
                case 40:
                    if (this.display.length && this.changeFocus(1)) {
                        event.stopPropagation();
                    }
                    break;
                case 13:
                    if (this.display.length) {
                        this.hide();
                        event.stopPropagation();
                        this.submit();
                    }
                    break;
                case 27:
                    if (this.display.length) {
                        this.hide();
                        event.stopPropagation();
                    }
                    break;
                case 9:
                    // If the user tabs out of the field, don't refresh.
                    return;
                }
            }
    
        };
    
        window.ListNav = ListNavigator
    
    })();
    
    var nav = new ListNav();
    nav.init({
        control: 'control',
        hardpoint: 'hardpoint',
        selector: 'item',
        choose: function(){
            console.log( arguments );
        }
    });
    nav.run();​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
This could be a duplicate question, but I have no idea what search terms

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.