• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Custom Select Box using CSS and JavaScript Tutorial

KappaChino

Application Lifecycle Manager
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
114
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you can learn how to create a simple Custom Select Box with Search Bar using CSS and JavaScript. The tutorial aims to provide students and beginners with a reference to learn with for creating a useful web application or website component. Here, I will be providing a sample web page script that demonstrates the creation of a Custom Select Box with a Search Bar.

What is Select Box?

The Select Box is an HTML Element used to create a drop-down list. It is usually used in a form with an input field that allows the user to select a value from multiple options. In HTML, we can simply create a select box using <select> tag, and the options are listed inside this tag enclosed in <option> tag for each item.

How to Create a Custom Select Box with Search Bar?

The Custom Select Box with Search Bar can be easily achieved using CSS and JavaScript. To do that, we need to create a new element that will represent the select box and hide the original select box. After that, we can design the select box using the CSS code. Then, using some JavaScript event listeners, methods, built-in APIs, and functions, we can make the custom select box functional and update the hidden original box value with the item selected on the custom one. Check out the sample web page script that I created and provided below to understand more about creating the custom select box.

Sample Web Page

The scripts below result in a simple web page that contains a custom select box. The custom select box contains a search feature that allows the user to search items using keywords. When the item is selected, the custom select box selected item text will be updated and the hidden select element will be also updated.

HTML

Here's the HTML file script of the page layout and select box element.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    CSS and JS - Custom Select Box</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  12. <link

    rel

    =

    "stylesheet"

    href

    =

    "custom-select.css"

    >
  13. </

    head

    >
  14. <body

    >
  15. <div

    id

    =

    "wrapper"

    >
  16. <div

    class

    =

    "page-title"

    >

    Creating a Simple Select Box using CSS and JavaScript</

    div

    >
  17. <hr

    style

    =

    "margin:auto; width:25px"

    >
  18. <div

    id

    =

    "select-wrapper"

    >
  19. <div

    >
  20. <select

    name

    =

    "sampleSelect"

    id

    =

    "sampleSelect"

    >
  21. <option

    >

    JavaScript</

    option

    >
  22. <option

    >

    Python</

    option

    >
  23. <option

    >

    Go</

    option

    >
  24. <option

    >

    Java</

    option

    >
  25. <option

    >

    Kotlin</

    option

    >
  26. <option

    >

    PHP</

    option

    >
  27. <option

    >

    C#</

    option

    >
  28. <option

    >

    Swift</

    option

    >
  29. <option

    >

    Ruby</

    option

    >
  30. <option

    >

    C and C++</

    option

    >
  31. <option

    >

    Matlab</

    option

    >
  32. <option

    >

    TypeScript</

    option

    >
  33. <option

    >

    Scala</

    option

    >
  34. <option

    >

    HTML</

    option

    >
  35. <option

    >

    CSS</

    option

    >
  36. </

    select

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >
  40. <script

    src

    =

    "custom-select.js"

    ></

    script

    >
  41. </

    body

    >
  42. </

    html

    >

Page Layout Stylesheet

Next, here's the CSS script that contains the code for the design of the page layout.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet'

    )

    ;
  2. :

    root

    {
  3. --secular-font

    :

    'Secular One'

    ,

    sans-serif

    ;
  4. }
  5. *

    {
  6. box-sizing

    :

    border-box

    ;
  7. }
  8. body *

    {
  9. font-family

    :

    'Rubik'

    ,

    sans-serif

    ;
  10. }
  11. /**
  12. Page Design
  13. */
  14. body,
  15. html{
  16. height

    :

    100%

    ;
  17. width

    :

    100%

    ;
  18. margin

    :

    0

    ;
  19. padding

    :

    0

    ;
  20. }
  21. body{
  22. background-color

    :

    #B9F3E4

    ;
  23. }
  24. /* Page Wrapper */
  25. #wrapper

    {
  26. width

    :

    100%

    ;
  27. padding

    :

    3em

    5em

    ;
  28. }
  29. .page-title{
  30. font-size

    :

    2.5rem

    ;
  31. font-weight

    :

    500

    ;
  32. color

    :

    #fff

    ;
  33. letter-spacing

    :

    3px

    ;
  34. font-family

    :

    var

    (

    --secular-font

    )

    ;
  35. text-align

    :

    center

    ;
  36. text-shadow

    :

    0px

    0px

    3px

    #2020208c

    ;
  37. }
  38. /* Select box Wrapper */
  39. #select-wrapper

    {
  40. position

    :

    relative

    ;
  41. width

    :

    300px

    ;
  42. margin

    :

    auto

    ;
  43. margin-top

    :

    3em
  44. }

Custom Select Box Stylesheet

Next, here's the CSS script that contains the code for the design of the custom select box.

  1. .custom-select-box-parent{
  2. position

    :

    relative

    ;
  3. width

    :

    100%

    ;
  4. }
  5. .custom-select-box-container{
  6. width

    :

    100%

    ;
  7. display

    :

    flex;
  8. flex-wrap

    :

    wrap;
  9. padding

    :

    .35em

    .5em

    ;
  10. background-color

    :

    #ffffff

    ;
  11. border

    :

    1px

    solid

    #eeeeee

    ;
  12. min-height

    :

    1.2em

    ;
  13. justify-content

    :

    space-between;
  14. box-shadow

    :

    1px

    1px

    3px

    #3d3d3d52

    ;
  15. cursor

    :

    pointer

    ;
  16. }
  17. .custom-select-box-selected{
  18. width

    :

    90%

    ;
  19. font-size

    :

    .9rem

    ;
  20. font-weight

    :

    400

    ;
  21. }

  22. .custom-select-box-dd-icon{
  23. width

    :

    10%

    ;
  24. transition

    :

    all .3s

    ease;
  25. position

    :

    relative

    ;
  26. }
  27. .custom-select-box-dd-icon

    :

    before

    {
  28. content

    :

    "\25BE

    "

    ;
  29. position

    :

    absolute

    ;
  30. top

    :

    0

    ;
  31. transform

    :

    translateX(

    50%

    )

    ;
  32. }
  33. .custom-select-box-parent

    .show

    .custom-select-box-dd-icon{
  34. transform

    :

    rotate

    (

    180deg

    )

    ;
  35. }


  36. .custom-select-box-options-container{
  37. width

    :

    100%

    ;
  38. position

    :

    absolute

    ;
  39. background-color

    :

    #fff

    ;
  40. border

    :

    1px

    solid

    #e7e7e7

    ;
  41. margin-top

    :

    .5em

    ;
  42. box-shadow

    :

    0px

    0px

    5px

    #2727272d

    ;
  43. display

    :

    none

    ;
  44. }
  45. .custom-select-box-parent

    .show

    .custom-select-box-options-container{
  46. display

    :

    block

    ;
  47. }
  48. .custom-select-box-search{
  49. position

    :

    relative

    ;
  50. width

    :

    100%

    ;
  51. padding

    :

    .35em

    ;
  52. }
  53. .custom-select-box-search

    :

    before

    {
  54. content

    :

    ""

    ;
  55. width

    :

    30px

    ;
  56. height

    :

    calc

    (

    100%

    - 0.7em

    )

    ;
  57. position

    :

    absolute

    ;
  58. z-index

    :

    2

    ;
  59. background

    :

    transparent

    url

    (

    "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' class='bi bi-search' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z'%3E%3C/path%3E%3C/svg%3E"

    )

    no-repeat

    center

    ;
  60. }
  61. .custom-select-box-search-box{
  62. width

    :

    100%

    ;
  63. padding

    :

    .35em

    0.35em

    .35em

    30px

    ;
  64. font-size

    :

    .9rem

    ;
  65. color

    :

    #333333

    ;
  66. outline

    :

    none

    ;
  67. border

    :

    1px

    solid

    #bbbbbb

    ;
  68. }
  69. .custom-select-box-search-box

    :

    focus

    {
  70. border-color

    :

    #a0a0a0

    ;
  71. box-shadow

    :

    0px

    0px

    5px

    #2727272d

    ;
  72. }
  73. .custom-select-box-options{
  74. position

    :

    relative

    ;
  75. margin-top

    :

    .35em

    ;
  76. width

    :

    100%

    ;
  77. max-height

    :

    35vh

    ;
  78. overflow-y

    :

    auto

    ;
  79. padding

    :

    .35em

    .2em
  80. }
  81. /* width */
  82. .custom-select-box-options

    ::

    -webkit-scrollbar {
  83. width

    :

    5px

    ;
  84. }

  85. /* Track */
  86. .custom-select-box-options

    ::

    -webkit-scrollbar-track {
  87. background

    :

    #f1f1f1

    ;
  88. }

  89. /* Handle */
  90. .custom-select-box-options

    ::

    -webkit-scrollbar-thumb {
  91. background

    :

    #888

    ;
  92. border-radius

    :

    5px

    ;
  93. }

  94. /* Handle on hover */
  95. .custom-select-box-options

    ::

    -webkit-scrollbar-thumb:

    hover

    {
  96. background

    :

    #555

    ;
  97. }
  98. .custom-select-box-item{
  99. background

    :

    #fff

    ;
  100. width

    :

    100%

    ;
  101. padding

    :

    .35em

    .5em

    ;
  102. border

    :

    1px

    solid

    #e9e9e9

    ;
  103. cursor

    :

    pointer

    ;
  104. }
  105. .custom-select-box-item

    :

    hover

    ,
  106. .custom-select-box-item[

    data-selected=

    "true"

    ]

    {
  107. background

    :

    #e6e6e6

    ;
  108. }


JavaScript

Next, here's the JavaScript object-oriented script that was created that creates the custom select box and handles the component's functionality codes.

  1. /** Custom Select Box */
  2. class

    CustomSelectBox{
  3. /** Parent Element */
  4. selectParentEl =

    document.createElement

    (

    'div'

    )
  5. /** Custom Select Box HTML */
  6. selectHTML =

    `<

    div class

    =

    "custom-select-box-container"

    >
  7. <

    div class

    =

    "custom-select-box-selected"

    >

    Please Select here</

    div>
  8. <

    span class

    =

    "custom-select-box-dd-icon"

    ></

    span>
  9. </

    div>
  10. <

    div class

    =

    "custom-select-box-options-container"

    >
  11. <

    div class

    =

    "custom-select-box-search"

    >
  12. <

    input type=

    "search"

    class

    =

    "custom-select-box-search-box"

    >
  13. </

    div>
  14. <

    div class

    =

    "custom-select-box-options"

    >
  15. </

    div>
  16. </

    div>

    `;

  17. optionItemEl =

    document.createElement

    (

    'div'

    )
  18. constructor(

    )

    {
  19. this

    .selectParentEl

    .classList

    .add

    (

    'custom-select-box-parent'

    )
  20. this

    .selectParentEl

    .innerHTML

    =

    this

    .selectHTML
  21. this

    .optionItemEl

    .classList

    .add

    (

    'custom-select-box-item'

    )
  22. this

    .selectParentEl

    .addEventListener

    (

    'click'

    ,

    this

    .showSelect

    .bind

    (

    this

    )

    )
  23. }
  24. init(

    el)

    {
  25. /** Initialize Custo Select Box */
  26. el.style

    .display

    =

    `none`;
  27. el.after

    (

    this

    .selectParentEl

    )
  28. var

    selOpts =

    el.querySelectorAll

    (

    'option'

    )

  29. /** Append Items */
  30. selOpts.forEach

    (

    option =>

    {
  31. var

    optEl =

    this

    .createElfromOption

    (

    option)
  32. this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-options'

    )

    .appendChild

    (

    optEl)
  33. }

    )

  34. /** Add Click Event for Selecting Item */
  35. this

    .selectParentEl

    .querySelectorAll

    (

    '.custom-select-box-item'

    )

    .forEach

    (

    item =>

    {
  36. item.addEventListener

    (

    'click'

    ,

    this

    .selectItem

    .bind

    (

    this

    ,

    item,

    el)

    )
  37. }

    )

  38. /** trigger Search */
  39. this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-search-box'

    )

    .addEventListener

    (

    'input'

    ,

    this

    .selectSearch

    .bind

    (

    this

    )

    )
  40. }

  41. /** Show custom option block */
  42. showSelect(

    )

    {
  43. this

    .selectParentEl

    .classList

    .add

    (

    'show'

    )
  44. document.addEventListener

    (

    'click'

    ,

    this

    .closeSelect

    .bind

    (

    this

    )

    )

  45. }
  46. /** close custom option block */
  47. closeSelect(

    e)

    {
  48. var

    selectBoxOptC =

    this

    .selectParentEl

    .outerHTML
  49. if

    (

    selectBoxOptC.includes

    (

    e.target

    .outerHTML

    )

    ==

    false

    )

    {
  50. this

    .selectParentEl

    .classList

    .remove

    (

    'show'

    )
  51. document.removeEventListener

    (

    'click'

    ,

    this

    .closeSelect

    )
  52. this

    .resetSearch

    (

    )
  53. }
  54. }
  55. /** Search Item */
  56. selectSearch(

    )

    {
  57. var

    search =

    this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-search-box'

    )

    .value
  58. this

    .selectParentEl

    .querySelectorAll

    (

    '.custom-select-box-item'

    )

    .forEach

    (

    item =>

    {
  59. if

    (

    (

    item.innerText

    .toLowerCase

    (

    )

    )

    .includes

    (

    search.toLowerCase

    (

    )

    )

    ==

    true

    )

    {
  60. item.style

    .display

    =

    'block'
  61. }

    else

    {
  62. item.style

    .display

    =

    'none'
  63. }
  64. }

    )
  65. }
  66. /** Reset Search box */
  67. resetSearch(

    )

    {
  68. this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-search-box'

    )

    .value

    =

    ''

    ;
  69. this

    .selectSearch

    (

    )
  70. }

  71. /** Selected Item function */
  72. selectItem(

    item,

    el)

    {
  73. var

    val =

    item.dataset

    .value
  74. var

    txt =

    item.innerText
  75. if

    (

    this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-item[data-selected="true"]'

    )

    !==

    null

    )
  76. this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-item[data-selected="true"]'

    )

    .removeAttribute

    (

    'data-selected'

    )
  77. item.dataset

    .selected

    =

    'true'
  78. if

    (

    el.querySelector

    (

    'option[selected]'

    )

    !==

    null

    )
  79. el.querySelector

    (

    'option[selected]'

    )

    .removeAttribute

    (

    'selected'

    )

  80. if

    (

    el.querySelector

    (

    'option'

    )

    !==

    undefined

    )

    {
  81. el.querySelectorAll

    (

    'option'

    )

    .forEach

    (

    option =>

    {
  82. if

    (

    !!

    option.dataset

    .value

    )

    {
  83. if

    (

    option.dataset

    .value

    ==

    val)
  84. option.setAttribute

    (

    'selected'

    ,

    true

    )

    ;
  85. }

    else

    {
  86. if

    (

    option.innerText

    ==

    val)
  87. option.setAttribute

    (

    'selected'

    ,

    true

    )

    ;
  88. }
  89. }

    )
  90. }
  91. this

    .selectParentEl

    .querySelector

    (

    '.custom-select-box-selected'

    )

    .innerText

    =

    txt
  92. setTimeout(

    (

    )

    =>

    {
  93. this

    .selectParentEl

    .classList

    .remove

    (

    'show'

    )
  94. document.removeEventListener

    (

    'click'

    ,

    this

    .closeSelect

    .bind

    (

    this

    )

    )
  95. this

    .resetSearch

    (

    )
  96. }

    ,

    100

    )

  97. }

  98. /** Create the Item form Option */
  99. createElfromOption(

    option)

    {
  100. var

    val =

    option.dataset

    .value

    ||

    option.innerText
  101. var

    txt =

    option.innerText
  102. var

    item =

    this

    .optionItemEl

    .cloneNode

    (

    true

    )
  103. item.dataset

    .value

    =

    val
  104. item.innerText

    =

    txt
  105. return

    item
  106. }
  107. }

Initialize Custom Select Box

Lastly, here's the JS script that initializes or converts the HTML select element to a custom select box.

  1. var

    customSelect =

    new

    CustomSelectBox(

    )
  2. customSelect.init

    (

    document.getElementById

    (

    'sampleSelect'

    )

    )

Snapshots

The following images or snapshot is the overall result of the web page scripts that I provided above.

Custom Select Box

css-js-custom-select-hidden-option.jpg


Option Block/Container is shown

css-js-custom-select.jpg


When Searching Item

css-js-custom-select-search.jpg


DEMO

css-js-custom-select.gif


There you go! I have also provided the source code zip file of the sample web page on this site and it is free to download. To download the file, you can simply click the download button located below this tutorial's content. Feel free to download and do some experiments.

That's it! I hope this Creating a Custom Select Box using CSS and JavaScript Tutorial will help you with what you are looking for and will be helpful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top