Creating accessible visual select in pure JavaScript without dependencies

How to create a visually more appealing select in pure JavaScript without any framework.


Let’s imagine we need a visual select that allows users to click on images or text tags for selecting multiple countries/hobbies/tags. We can put a classic HTML <select> tag to do the job, but we want to delight the users with a more appealing visual selecting where we directly click on the options to select or deselect them. Also we might want to provide a simple input filter if there are too many options to display. On top we want this to be a general, accessible and maintainable solution.


To solve the problem we start with general HTML that works and can be used in case the JavaScript is turned off on the page:

Our activities select is ready and working. Yes, HTML is not broken until we break it with JavaScript. The idea behind keeping it accessible is to hide this select with JavaScript (and not default CSS) and replacing the select management with our own code. I also put two more divs there; one for filtering the options and one where we build our own visually pleasing select.

The idea of maintainability is to build our JavaScript select as function that can be used for many other selects. The public API that we will strive for is as follows:

In terms of our example we would initialize our VisualSelect function with the div IDs above:

Having our function API ready we can start with an actual coding. Let’s lay out some foundation first:

First thing you notice is that I am using var for declaration. This is because we are writing ES5 and not ES6 to avoid a compilation step but keeping compatibility for older browsers. After we prepare our variables representing the elements in the document object model we initialize our select and filter. Select initialization is about building possible options from the actual HTML select with addSelectOption. Filter initialization is about setting oninput event handling with showOrHideOptionButton.

Let’s look and each function and its implementation:

In the virtual select initialization we just need to go through all <select> options. We simply go through all of them and add them with addSelectOption function:

Here we create our option link in our virtual select. We create the link element with document.createElement function, put the option text as the link text and add an onclick event handler that will enable or disable the real select option (function below). Even though I call this select virtual we are actually modifying the DOM to include these option links. After that we set the initial state for the option by appending selected class if the option is already selected. Once we have our option ready we append it in our virtual select div and add the link to the links array which tracks all the links present.

enableOrDisableOption does a simple toggle on the selected option state. It has to change both the visual appearance of the link and the actual state of the option. Once done we clear the search filter since it’s clear the correct option was found.

Now for the filter:

The initialization is obvious. We assign showOrHideOptionButton to run whenever an input changes its value.

In showOrHideOptionButton we asks on the input’s current value with this.value. Since this function is called on our input this points to the input element. Then we go through our links that represents the link options and simply either show them or hide them by appending a hidden class (we use a class for convenience of different styling such as blurring out the element). Notice that indexOf determines if we got a match or not.

Finally we have a function that brings all our virtual option links back to life and resets the filter value.


We have highly compatible, reusable, accessible and fast solution for our new shiny select with a little bit of ES5 and DOM manipulation.

Here is the final JS fiddle.

There are of course many other ways how to do this, for example one can have a function returning a JavaScript object so that we could manually call mount (for initializing) and unmount when needed. I also did not include images for the option links, but that’s a very simple exercise.

Update: Updated a little based on nice feedback from wizao.

Leave a comment

Your email address will not be published. Required fields are marked *