Dropdown Selection Box for list.js

First of all let me introduce you to a really handy JavaScript called list.js and what it does is allows you to have a dynamic list of any items you want in a static HTML page without any use of database. And the implementation of this script is really very easy, all you need to do is make your list of items in tables just like you do in HTML and then just add a class reference to your table and include this list.js in header and you are done. For more info visit list.js website to know more about it and I would recommend to download it and use it and then come back to this article. If you are already familiar with this script then proceed to read further.

When you create a dynamic list with this script it allows to search and filter the list using an inbuilt search box and sort buttons. But what if we need a drop down selection option for our list, for example we have a big list of restaurant names according to city and we want to filter the list by selecting city list in drop down? In list.js all we got for this is a search box and we have to manually enter the city name in search box every time which is not the best option. Because I am lazy and always go for the easiest route here is what I planned to do: Make a selection box with cities name and when someone select the city from drop down, the value of city would go straight into the search box and this will filter our list.

So first of all we need a selection box HTML code and a javascript that will take value from selection and feed it to search box.

Okay so the above code will give you a drop down list. You will notice class, name, id and onchange value given for the select box. Out of these the value of id and onchange is important, rest is optional.  We will now use the id value (filter) and onchange function (changleValue) in a javascript that will pull value from selection and feed it to list.js search box. You need to put this script in header before the </head> tag.

 

I have used 3 city names but you can add as many as you like by adding more “else if” statement. The last “” blank value is for – select city – value in drop down so that when user selects this value, it displays all the cities. You will notice that I have used focus() function after feeding the value and this is to make the cursor go in the search box to mimic actual typing in the search box otherwise search box will not detect the text entered by our javascript. Now every time you select any option from the drop down, its value will go in the search box and since its an ajax search it should automatically filter our list now. Because search box is useless you may want to hide it from users now which you can do by using this css code:

No we could not use visibility hidden or display none here because that will remove the search box totally from HTML code and that will result in our drop down not working, we want this search box in HTML code but just not visible to users.

All seems to be ready now however when tested this code didn’t worked for me. Reason was that list.js was using “keyup” function for triggering search box and we are using “focus””. So what you need to do is open up list.js file and search for “keyup” and replace it with “focus”. Now your script is ready and dropdown selection should work fine!

You can download a working Demo of above example here.

Mohd. Hashim Khan

I work as a freelance web designer and developer and enjoys fiddling with machines and electronics stuff. I am also working as a part time computer teacher at Aligarh Muslim University. I share my knowledge on various topics here so that others can make use of it and take benefit from my experiences.

You may also like...

2 Responses

  1. GT says:

    Do you have something like this with the search bar intact? I want to filter and search to both be available to be used. Thanks!

  2. Omar says:

    How can you hide the text the function searches for? Like have it search & filter by a class?

Leave a Reply

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