assign a key for browsing through results

I have an input field and I am using ajax to fetch results. Here is a simplified version of the code:

<input placeholder="search here" id="search"></input>
<div id="searchResults">
     <a href="link">search result 1</a>
     <a href="link">search result 2</a>
</div>

I would like for users to be able to navigate through the search results using arrow keys after they have entered a search query, without having to move the mouse or use the tab key.

Answer №1

Here is an illustration for reference. View the demonstration http://example.com/demo/code

$(document).ready(function () {


    $(document).keyup(function (e) 
     {
        var key = (e.keyCode ? e.keyCode : e.which);
         if (key == 38) {
             $("#searchResults a").css("background-color","#fff");
            $("#searchResults a").prev().css("background-color","yellow");
         } 
         else if (key == 40) 
         {
             $("#searchResults a").css("background-color","#fff");
            $("#searchResults a").next().css("background-color","yellow");
         }
    });

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jasmine with Array.from is not a compatible combination

Attempting to utilize Jasmine for testing my code has presented a challenge. The code functions properly in the browser, and Array.from() also works seamlessly in node, as demonstrated below: > t = [1, 2, 3] [ 1, 2, 3 ] > Array.from(t) [ 1, 2, 3 ] ...

How can I write an if-else statement in JavaScript with Mongoose for MongoDB?

I am facing a challenge where I need to execute a statement only if the object is not null. If the object is null, I should skip executing anything. Is there a correct way to achieve this? I attempted it on MongoDB Playground but unfortunately, it did not ...

What could be causing my socket to enter a never-ending loop?

Client Side Code: useEffect(() => { socketRef.current = io.connect("...", { transports : ['websocket'] }) socketRef.current.emit("online", id) socketRef.current.on("message", ({ name, message }) => ...

What is the best way to create a carousel component with this particular design?

`I want to enhance my static HTML CSS website by incorporating a slider feature using JavaScript. Looking for guidance on the best approach. Here is the layout. I've attempted various tutorials but they weren't compatible with my layout or reli ...

Steps for activating mouse dragging in a div that supports drag and drop functionality

I have a div containing multiple elements, and I need the ability to drag and drop the entire div with the attribute draggable=true. However, one of the elements in the div is a bar graph that allows users to select a specific range by dragging their mouse ...

Search, retrieve, and sort data from the database based on selected checkboxes

I am in the process of developing a search page that will enable users to search for candidates based on specific skills they possess. The front end of the search page contains various checkboxes for skills. I am using a function to store the values of th ...

Patience is required to await the response of the AJAX call before binding it

There is a getGeneral function that makes an ajax call using the GET method. Once the data (in json format) is received by ajax, it creates a Knockout model from the given json and returns the created Knockout model. After the Knockout model is created an ...

Enhance the keyup search function to accept the input value as well

I currently have a keyup search function that works when a user types directly into the field. Here is the code snippet: if(this.$search_ele.length){ this.has_search = true; this.searchFn = this.buildSearchFn(opts.fields); this.bindEv ...

h1 tag set for jQuery AJAX activation

Currently working on a website that heavily relies on ajax. Encountering an issue that hasn't been solved through online resources. Now, I'm sharing my function for fetching a page: function loadTemplate(name, replaceWholePage = true){ $.wh ...

Setting the height of Material-UI TreeView

Looking for advice on how to set a fixed height or dynamically adjust the height of a material-ui TreeView to fit the display? https://i.sstatic.net/40Qlv.png I'd like a scrollbar to appear when the TreeView content reaches a height of 100 pixels, f ...

Optimizing React+ReactRouter web app loading times by efficiently splitting and loading CSS using WebPack

I am encountering a delay in the initial loading of my web app, even after implementing various improvements like using only critical CSS during server side rendering. Unfortunately, post-SSR, React still generates unnecessary CSS rules that are not utiliz ...

How to place an element in a specific location within the DOM using JavaScript

How can I position a created element in a specific place within the DOM using this code? Currently, it appends at the bottom of the page. var x = document.getElementById('options_10528_1'); var pos = document.getElementById('options-10528- ...

Storing the value from the INPUT field in the state of React is not permitted

I'm attempting to retrieve the user input value from the INPUT field and update it in the corresponding state. However, no matter what I type in the field, it doesn't get set to the state. createForm(x){ var dx = 'd'+x let da ...

What is the method to determine the encoding that the user is using to input into the browser?

After reading Joel's insightful article about character sets, I have decided to follow his advice by using UTF-8 on both my web page and in my database. However, one thing that confuses me is how to handle user input. As Joel aptly points out, "It doe ...

Is it not possible to make updates while the state is already in transition?

this.state = { filterData: [{ attribute: '-1', filter: '-1', value: '' }], } _createFilterUI(dataSourceColumns) { if (this.state.dataSourceIndex == -1) return <div > Kindly first select Data Sourc ...

I'm fresh to the world of Angular and I'm looking to display a modal with form details to confirm before submitting the form

Instead of immediately submitting the details when signing up, I would like to show the form details filled by the user in a modal first. The user can then choose to submit the form or go back and edit the details. Using Angular, how can we implement a fe ...

Updating to a newer version of jQuery causes issues with pop-out submenus

Looking for a way to create a menu with pop-out submenus? Here's an example using jQuery: <script type="text/javascript"> $(document).ready(function() { var hoverAttributes = { speed: 10, delay: 1 ...

Talebook: Unable to modify UI theming color

As I embark on creating my own theme in Storybook, I am closely following the guidelines outlined here: Currently, I have copied the necessary files from the website and everything seems to be working fine. However, I am facing an issue when trying to cus ...

Ways to expand the border horizontally using CSS animation from the middle

Currently, I am experimenting with CSS animation and I have a query regarding creating a vertical line that automatically grows in length when the page is loaded. I am interested in making the vertical line expand from the center in both upward and downwar ...

Unable to align Flexbox item to flex-end position

Can someone help me troubleshoot this codepen with the .inner-wrp div (dark red) fitting under the lightblue .top-right div? Check it out here! .purple { background: purple; } .green { background: green; } .lightblue { background: lightblue; } ...