Exploring a List of Divisions Based on User Input

My input field has a special functionality - Pressing the down arrow key will shift focus from the input to the first element in the list of divs below it, moving downwards as you continue pressing the down key. Pressing enter will set the text in that div as the input value. The same goes for the up arrow key, but in reverse - shifting focus to the last element in the list and moving upwards with each press of the up key.

Experience this feature in action on my fiddle: Example

Answer №1

$(function(){
    var $container = $('#list').children();
    var pointer = 0;

    function decrease(){
        if(pointer == $container.length - 1) pointer = 0;
        else pointer++;           
        moveElement();
    };
    function increase(){
        if(pointer == 0) pointer = $container.length - 1;
        else pointer--;           
        moveElement();
    };

    function selectValue(){
        var selectedVal = $($container[pointer]).html();
        $('inputBox').val(selectedVal);
    };

    function moveElement()
    {
        $container.removeClass('selected');
        $($container[pointer]).addClass('selected');
    };

    // highlights first item
    moveElement();

    $('inputBox').on('keydown', function(e){
        switch(e.keyCode)
        {
            case 13:
                selectValue();
                break;
            case 40:
                decrease();
                break;
            case 38:
                increase();
                break;
        }
    });
});

Check out the updated code on JSFiddle - http://jsfiddle.net/tt0nnzvm/4/

EDIT: Fiddle and code have been revised.

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

Align the timing of the animation with the dataset in ThreeJS

I am faced with the challenge of working with data that includes time in milliseconds and the x, y, z position of an object at each specific time interval. msec |pos_x |pos_y |pos_z ------------------------------ 0 |318 |24 |3 25 |3 ...

Can the Three.js Orbit Controls be customized to modify their appearance?

While I appreciate the orbit controls that come with Three.js, I am wondering if there is a way to customize them to follow the path of an ellipse (or technically an ellipsoid) instead of a circle. My main goal is to move the camera in an ellipse using th ...

Difficulty in loading TypeScript web component using Polymer serve

As I follow the LitElement guide provided here: , I meticulously adhere to each step mentioned and then execute polymer serve in my project's root directory. I diligently clone every file from their example stackblitz pages. Is it possible that using ...

Tips on retrieving the value of a dynamically created control in ASP.NET c# prior to page loading

Is there a way to retrieve the value of a dynamically created control before the page loads in ASP.NET using C#? protected void Page_PreInit(object sender, EventArgs e) { int i = 0; //createDynamicControl(i); var elems = Request.Form.AllKeys.W ...

Utilizing Bootstrap Modal to Display PHP Data Dynamically

Modals always pose a challenge for me, especially when I'm trying to work with someone else's code that has a unique take on modals that I really appreciate (if only I can make it function correctly). The issue arises when the modal is supposed ...

Interdependent function calls between two functions

When faced with the task of recursively checking two objects with sorted keys, I came up with a solution involving two functions. buildObj - this function retrieves all unique keys from the objects, sorts them, and then calls buildObjKey for each key bui ...

Component Not Rendered - React Native

I am currently working on retrieving data from an API using two functions and constants: const [isLoadingRoom, setLoadingRoom] = useState(true); const [isLoadingLobby, setLoadingLobby] = useState(true); const [rooms, setRooms] = useState([]); ...

Utilizing AngularJS routes to load a specific URL when accessing a page for the first time

Working on developing a Single Page Application using AngularJS, my configuration settings appear as follows: app.config(["$routeProvider", function($routeProvider) { return $routeProvider .when("/", { redirectTo: " ...

Creating a CSV file using an AJAX request in PHP

In my current project involving PHP AJAX DATATABLE within the Drupal framework, I have successfully developed a function to create a CSV file from table records. This function is triggered by clicking a button in HTML or accessing a specific URL. The PHP ...

Create a feature that allows users to view photos similar to the way it

I want to incorporate a Twitter feed on my website that displays images posted. Instead of having the images redirecting users to Twitter when clicked, I would like them to reveal themselves underneath when a link labeled "View Photo" is clicked, and then ...

Deciphering the outcome of the splice method in CoffeeScript

Recently, I have been utilizing CoffeeScript in conjunction with the JS splice function. From my understanding, the JS splice function should return the objects that were spliced out and modify the original array. While this concept works smoothly with sim ...

What is the reason the <line> tag is not functioning properly when used within the <clipPath> element?

Here's an example using: <circle> <svg viewBox="-20 -20 150 150" xmlns="http://www.w3.org/2000/svg"> <clipPath id="myClip" clipPathUnits="objectBoundingBox"> <circle cx=".5" cy=".5" r=".5" /> </clipPath> < ...

Using Next.js to pass fetched data as props to components

I am currently working on integrating a leaflet map into my Next.js project. The map display is already functioning with predefined latitude and longitude in the component. However, I now need to show data retrieved from my API as the longitude and latitu ...

Are both fading in and adjusting the height?

Can an element be faded in and have its height set simultaneously? if (scrollPosition > 50) { $("header").fadeIn("slow"); $("header").css("height", "50px"); } ...

What is the method to obtain an object as the return value from a click function in JavaScript?

I would like to retrieve the cell value from a table by clicking on a button. I have already created a function called getDetail in JavaScript that is implemented on the button, but I am facing difficulty in returning the value from the function. <butto ...

Issue with data decimation in Chart.js, encountering problems with parsing

I'm facing a challenge while plotting a graph using chart JS with approximately 300,000 data points. The performance is slow, so I am exploring ways to enhance it by utilizing the data decimation plugin. However, the data doesn't seem to be getti ...

Ensure the bottom div is anchored with a top margin

I am attempting to design a contact/information section at the bottom of my webpage, similar to the grey area at the bottom of this site. Here is my HTML code: *{margin:0; padding:0; box-sizing: border-box;} body{ padding-bottom: 0; } body .main{ marg ...

Adjust HTML table cell height to dynamically match content and align to top

I am currently working on creating a table layout where the left column adjusts its height based on content, while the right column needs to have a fixed height of 300. <table border="1" cellspacing="0" cellpadding="0"> <tr> <td>&a ...

`Upkeeping service variable upon route alteration in Angular 2`

Utilizing a UI service to control the styling of elements on my webpage is essential. The members of this service change with each route, determining how the header and page will look. For example: If the headerStyle member of the service is set to dark ...

What could be the reason for the malfunction of Twitter Bootstrap's typeahead feature in this case?

Struggling to implement typeahead.js into my current project. Despite having bootstrap loaded, the source code does not mention anything about typeahead. As a result, I included the standalone js file with hopes of making it work. Upon implementation, the ...