Select Box with Search Option using jQuery
In this tutorial, I will explain how to create a select box with search option using jquery, you must have seen on various websites that when you open html select box, it also provides you an option to write in input field to make your selection easier. This enhance the user experience of your web application.
I will use the select2 library to implement select box with search option, this require a jquery library, so we will need to include jquery library.
jQuery Library
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
Add the above library in the footer section, ideally should be before closing </body> tag.
jQuery Select2 Library & jQuery Script
<script src="select2.min.js"></script>
<script>
$("#country").select2( {
placeholder: "Select Country",
allowClear: true
} );
</script>
Include the above select2 library after jquery library and before closing </body> tag.
HTML
Create a select box with your desired select options, in here i am using country select example, following are just few countries name, you can add as much you want.
<select id="country">
<option value="">Select Country</option>
<option value="586">Pakistan</option>
<option value="682">Saudi Arabia</option>
<option value="784">United Arab Emirates</option>
<option value="826">United Kingdom</option>
<option value="840">United States</option>
</select>
As you can see in above code that i have given id name #country, which we will use to implement search option in select box.
CSS
Include this CSS in your head section.
<link rel="stylesheet" href="select2.min.css" />
You can download both CSS and Select2.js from the download file in the below link:
In case if you face input box alignment issue, then you can add the following CSS to fix alignment issue.
.select2-dropdown {
top: 22px !important;
left: 8px !important;
}
I tried my best to explain it, if anyone still have any problem then you can leave your comment in the below comment section.
If you found this tutorial helpful, share it with your friends and developers group.
Facebook Official Page: All PHP Tricks
Twitter Official Page: All PHP Tricks
While using this code. Donot get the output.
Aan, you will need to make sure in your console that there is no error regarding ajax library and select2.min.js.
If any of these files are not included then it will not work, you can download code and try to execute the code.
Hope this will solve your issue.
Thank you very much !
You are the best!!!!!
You are most welcome Alex.
Hi Javed,
I used the scripts that you mentioned in this article, it is not working for me. It is appearing just like the default select box. There is no error in the console. I’m using jquery 3.4.1.
Thanks
Make sure that your select2.min.js and css is linked properly, if your provided URL for these library is incorrect, it will not work.
Hi Javed, Im not able to search on dynamic data. i.e. If data is coming from api. It’s working on static option but not on dynamic option.
Thank you so much. Last 3 days I searched for this kind of searching option.
You welcome Fatema.
thank you sir
You welcome.
Super Trick
Thanks
Thanks a lot Brother
You welcome Sumit.
Hi Mr.Rehman
If i want to multiple select, what can i do?
Hi Rakoto, select the options by pressing CTRL button.
I need to find out the value of country with country name is possible but it is not been selected in search
Hi Suresh, well you will always get the value of these countries upon form submission but if you also want the country name, then you have two ways, either on the form submission query the database table of country and get the country name or you can set hidden input field in your form and on the change country event, get the country name using the $(“#country”).text(); and pass its value to your hidden field and get the value of that field on form submission, hope this will help you.
I’m not able to write text in search box when used with popup
I think you are facing z-index issue, you should learn little more about z-index in css to control the layers.
Search box is not appearing
Kindly check the demo, it is working fine there, may be jquery is not loading at your end. Make sure jQuery library is working fine there.
Thank you very much!!!
Your tutorial helped me!
You welcome 🙂
Hi
Very nice tools
Any ideas how I can increase the height of select2-results. It only shows few results (as for the example, only 5).
Hi Jean-Paul, thanks for liking it.
Well as you can see that we are using library and it has its own css, all you need to overwrite the css of library in your file, using !important with the same class which has fixed the height of that box. You can use browser code inspector to find the class.