What is the method for incorporating an upward arrow into a select element, while also including a downward arrow, in order to navigate through options exclusively with

I have a select element that I have styled with up and down arrows. However, I am seeking assistance to navigate the options dropdown solely using these arrows and hide the dropdown list.

Here is the HTML:

    <div class="select_wrap">
       <div class="select_arow_wrap">
          <span id="select-up" class="select-up"></span>
          <span id="select-down" class="select-down"></span>
       </div>
       <div class="select_bg">
           <select id="select">
               <option value="Newzealand">Newzealand</option>
               <option value="India">India</option>
               <option value="United States">United States</option>
               <option value="United Kingdom">United Kingdom</option>
           </select>
       </div>
    </div>

The CSS:

    .select_bg { 
background-image:url(../images/select-bg.png); 
background-repeat:repeat-x; 
border:1px solid #dddedf; 
background-position:left center; 
background-color:transparent; 
width:100%; 
border-radius:5px; 
padding:5px; 
height:33px;
}

 .select_wrap { 
position:relative; 
float:left; 
width:100%; 
overflow:hidden;
margin:5px;
}
.form_wrap form select { 
background-color:transparent; 
width:105%; 
padding:3px; 
top:0; 
position:absolute;
-moz-appearance:none;
-webkit-appearance: none;
appearance: none;
margin:0;
height:30px;

}

.form_wrap form select option {
display:none;   
}
.form_wrap form select::-ms-expand {
display: none;
}
.select_arow_wrap { 
border-left:1px solid #dddedf; 
float:left;
position:absolute;
width:16px;
height:30px;
right:5px;
top:2px;
padding:2px;
z-index:999;
}
.select-up,
.select-down{   
background-repeat:no-repeat;
float:left;
width:14px;
height:13px;
cursor:pointer;
}

.select-up { 
background-image:url(../images/select_up_arw.png); 
right:-10px; 
top:6px;
}
.select_wrap .select-down { 
background-image:url(../images/select_dnw_arw.png); 
right:-9px; 
bottom:8px;
}

View the design here:

I am looking for a jQuery solution to navigate the options by clicking the arrows. Thank you in advance!

Answer №1

Here is a sample code snippet you can use:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<style>

  img{
    width : 20px;
    height : 20px;
}

  </style>
<script>

$(document).ready(function(){
    var nextListitem;
        var noOfListItems = $("#select > option").length-1;
        var curListItem = $("#select")[0].selectedIndex;
        $("#upArrow").on("click", function(){
            //alert(noOfListItems);
             // Decrement the selection by one, unless that will be less than zero, then go to the last option
            nextListitem = (curListItem-1 < 0) ? noOfListItems : curListItem-1;
          //  alert(nextListitem);
            curListItem = nextListitem;
            $("#select")[0].selectedIndex = nextListitem;
        });
        $("#downArrow").on("click", function(){
          //  alert(noOfListItems);
             // Increment the selection by one, unless that will be more than the number of options, then go to the first option
           nextListitem = (curListItem+1 > noOfListItems) ? 0 : curListItem+1;
         //   alert(nextListitem);
            curListItem = nextListitem;
            $("#select")[0].selectedIndex = nextListitem;
        });



});


  </script>
<body>
 <div class="select_wrap">
       <div class="select_arow_wrap">
          <span id="select-up" class="select-up"></span>
          <span id="select-down" class="select-down"></span>
       </div>
       <div class="select_bg">
           <select id="select">
               <option value="Newzealand">Newzealand</option>
               <option value="India">India</option>
               <option value="United States">United States</option>
               <option value="United Kingdom">United Kingdom</option>
           </select>

        <img id="upArrow" src="http://www.clker.com/cliparts/8/7/Y/H/W/e/up-arrow-circle-hi.png" style="float : top" />
        <img id="downArrow" src="http://www.clker.com/cliparts/y/m/X/o/s/R/down-arrow-circle-hi.png" style="float : bottom" />

       </div>
    </div>

</body>
</html>

Check out a demo here:Fiddle

Answer №2

To create a unique customized element that functions as a selection element with personalized features, you can use jQuery's click event to modify the content of your custom select element. Here's a demonstration of how you can achieve this: (Feel free to customize the styling as desired.)

HTML :

<div id="myCustomSelectElement">
    <div id="selectionBox">
        <input type="text" id="mySelector" readonly />
    </div>
    <div id="navigator">
        <img id="upButton" src="http://www.clker.com/cliparts/8/7/Y/H/W/e/up-arrow-circle-hi.png" style="float : top" />
        <img id="downButton" src="http://www.clker.com/cliparts/y/m/X/o/s/R/down-arrow-circle-hi.png" style="float : bottom" />
    </div>
</div>

jQuery :

var items = ["Paris", "London", "New York"];
var index = 0;
var currentSelection = items[index];
$("#mySelector").val(currentSelection);

$("#upButton").on("click", function(){
    if(index > 0){
        index--;
    }
    setItem(index);
});

$("#downButton").on("click", function(){
    if(index < items.length - 1){
        index++;
    }
    setItem(index);
});

function setItem(index){
    currentSelection = items[index];
    $("#mySelector").val(currentSelection);
}

jsFiddle

Answer №3

Implement this script to enable select box navigation

    var items = new Array();
    $("#select option").each(function(){
        items.push($(this).val());
    });
    var index = 0;
    var currItem = items[index];


    $("#select-up").on("click", function(){
        if(index > 0){
            index--;
        }
        updateSelection(index);
    });

    $("#select-down").on("click", function(){
        if(index < items.length - 1){
            index++;
        }
        updateSelection(index);
    });

    function updateSelection(index){
        currItem = items[index];
        console.log(currItem);
        //$('#selector option[value="'+currentElement+'"]').attr("selected",true);
        $('#select').val(currItem)
    }

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 Angularjs: Trigger Directive Execution Post Completion of ng-init Function

I'm facing an issue with calling a function in ng-init within my HTML file. The function is responsible for making an API call and collecting data, which is then assigned to a scope variable and passed to a directive. Initially, the controller gets e ...

Conceal and unveil stationary navigation when scrolling back to the very top of the screen

My current setup includes a navigation that dynamically hides as the user scrolls down and reappears when scrolling up. Additionally, I have applied the class .fixed on #top-wrapper to alter the layout/styling of this specific div. However, I am facing dif ...

Step-by-step instructions for creating a shadow page and displaying a loading div when an ajax call is made

Is it possible to shadow the entire page when a user clicks a button, display a div in the center of the page with a loading gif, and then make an ajax request to a page? If so, can someone explain how to achieve this? I found here, the following code: ( ...

Controlling LED lights using Johnny-Five and Arduino in NW.js

I have a setup with my board that includes two buttons (each with pull-up resistors) and two LEDs. My goal is to make each button activate its corresponding LED while deactivating the other one. Here's the code I'm using: var five = require(&ap ...

Creating a straightforward game using node.js, socket.io, and jquery, where users can take turns participating

I am currently working on a game using node.js, socket.io, and jquery. The game involves two users logging in from different locations and browsers. When the first user logs in to play, they receive a message saying "Your Turn" and have 30 seconds to make ...

Tips for positioning the search bar on the opposite side of the navbar in Bootstrap 5

In my navbar, I have items aligned on the left and a search bar aligned on the right. However, when I try to move the items to the right using the ms-auto class, only the items move and the search bar stays in place. How can I adjust the alignment so that ...

I'm looking for a way to automatically execute the script in my .json file every time I open my index.html page. Can anyone help me

Inside the .json file, I have a script section that requires me to manually run it everytime by typing npm run (the script's name) in the terminal when I want to open my html page on the server. Is there a way for this process to be automated so that ...

The sender parameter in runtime.onMessage does not include the tab information

I've implemented a browser action that triggers a message to be sent. chrome.browserAction.onClicked.addListener(function(tab) { var message = { 'message': 'overlay-intent' }; tab_message(tab.id, message); }); ...

Automated tools and frameworks for the testing of website performance

Hello, I have a website that heavily relies on AJAX for server communication. I am looking to conduct performance and stress testing using automated scripts. Can you provide any suggestions or recommendations? I am specifically interested in the functiona ...

Convert a form into plain text utilizing CSS with minimal usage of jQuery or Javascript

I am working on an HTML page which has a form with various tags such as checkboxes, dropdowns, and more. When a button is clicked, I want to display the same form as plain text in a popup using jQuery dialog. Currently, I have managed to show the form in ...

"Implementation of Google+ button causing the appearance of a horizontal scrollbar

Adding Facebook and Twitter sharing buttons was easy, but now I'm having trouble with Google+. No matter where I place the code on my page (using a Bootstrap grid), it always adds 2-3 pixels on the right side, creating a horizontal scrollbar: <div ...

I encountered an issue where the error "data.map is not a function" occurs whenever I try to execute a React component

I have implemented the HorizontalScrollbar component and passed data as a prop. When I try to run the HorizontalScrollbar component, I encounter the following error in the console tab of Chrome: Error: Uncaught TypeError: data.map is not a function Horiz ...

The error "localStorage is not defined when using an axios interceptor in NextJS"

Within my root directory, there lies a file named api.js. This particular file is responsible for managing calls to an external API, with a focus on request and response interceptors. One specific use case involves injecting the access_token, leading to th ...

Can Three.js be used to create a compact canvas?

I've successfully implemented a three.js scene on my website where users can drag to rotate an object. However, I don't want the scene to take up the entire webpage. I tried adjusting the field parameters with the following code: renderer.setSiz ...

The 'classProperties' React component module is currently not enabled

Encountering an error while running my react module 'classProperties' isn't currently enabled (44:11): } 43 | // componentDidUpdate or try this > 44 | onClick = (e) => { | ^ 45 | e.preventDefault(); 4 ...

Reload a forum in a div without refreshing the entire page

I'm interested in finding out if it's possible for a div to reload similar to how a window does. I have a forum set up, but I don't want the entire page to reload after each form submission. Instead, I'm wondering if only the elements w ...

JsTree throws an error stating that the JSON is invalid

I am in the process of creating a Jstree and here is the code for it: $(function () { $("#groups") .jstree({ "plugins" : [ "themes", "json_data", "ui", "crrm", "cookies", "dnd", "search", "types", "contextmenu" ], "json_data" : { ...

Discovering HTML attributes using part of the attribute's name

Can someone help me identify and remove all attributes in my HTML page that contain the word "data"? I want to clear out the values for these attributes. Any suggestions on how to achieve this? For instance, consider the following snippet from my HTML: & ...

What is the best way to utilize RxJs for streaming HostListener events?

Although I've found plenty of resources on binding Angular HostListeners, I'm curious about using RxJs to stream it instead: @HostListener('document:click', ['$event']) handleClick(event: Event) { // etc } I want to cre ...

Center align a font-awesome icon vertically next to a paragraph of text

Is there a way to align a font-awesome icon on the left side of a text paragraph while keeping the text on the right side, even if it's longer and wraps underneath the icon? I've tried various code snippets but haven't found a solution yet. ...