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

Creating a Live Filter with Multiple Options using Checkbox with jQuery

Goodnight

Play-to-Earn Innovator
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
55
Likes
161
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we are going to create a Live Filter with Multiple Options using Checkbox with jQuery. Many projects or systems need this kind of function for fast filtering the data from the database. Filter functions are very common to us, in other cases when you use the filter function it also loads the whole page. In our case, we are going to create a live filter with multiple options using the checkbox without load the whole web page. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out.

Let's start coding.

Getting Started

First of all, we have to download the jQuery Library. Click here to download the library. You can also use jQuery Library's CDN but you must have an internet connection when executing your project if you use CDN's.

For the design, I will use the following CSS.

  1. /* latin */
  2. @font-face

    {
  3. font-family

    :

    'Droid Sans'

    ;
  4. font-style

    :

    normal

    ;
  5. font-weight

    :

    400

    ;
  6. src:

    local(

    'Droid Sans'

    )

    ,

    local(

    'DroidSans'

    )

    ,

    url

    (

    http:

    //fonts.gstatic

    .com/s/droidsans/v6/s-BiyweUPV0v-yRb-cjciPk_vArhqVIZ0nv9q090hN8.woff2)

    format(

    'woff2'

    )

    ;
  7. unicode-range

    :

    U+

    0000-00FF,

    U+

    0131,

    U+

    0152-0153,

    U+

    02C6,

    U+

    02DA,

    U+

    02DC,

    U+

    2000

    -206F,

    U+

    2074

    ,

    U+

    20AC,

    U+

    2212

    ,

    U+

    2215

    ,

    U+

    E0FF,

    U+

    EFFD,

    U+

    F000;
  8. }

  9. body {
  10. background

    :

    #9e9e9e

    ;
  11. }
  12. .container

    {
  13. width

    :

    800px

    ;
  14. height

    :

    auto

    ;
  15. margin

    :

    0

    auto

    ;
  16. }
  17. .filterblock

    {
  18. display

    :

    inline

    ;
  19. margin-right

    :

    15px

    ;
  20. }
  21. #filters

    {
  22. background

    :

    #487479

    ;
  23. padding

    :

    10px

    ;
  24. border-radius

    :

    3px

    3px

    0

    0

    ;
  25. text-align

    :

    center

    ;
  26. }
  27. .resultblock

    {
  28. width

    :

    100%

    ;
  29. height

    :

    auto

    ;
  30. background

    :

    #487479

    ;
  31. color

    :

    white

    ;
  32. margin-top

    :

    15px

    ;
  33. font-family

    :

    cursive

    ;
  34. }
  35. .itemimg

    {
  36. width

    :

    150px

    ;
  37. height

    :

    150px

    ;
  38. background

    :

    #ccc

    ;
  39. }

  40. .desc

    {
  41. width

    :

    650px

    ;
  42. height

    :

    auto

    ;
  43. display

    :

    inline-block

    ;
  44. float

    :

    right

    ;
  45. }

  46. .desc_text

    {
  47. padding

    :

    10px

    ;
  48. }

  49. label {
  50. display

    :

    inline-block

    ;
  51. cursor

    :

    pointer

    ;
  52. position

    :

    relative

    ;
  53. padding-left

    :

    25px

    ;
  54. margin-right

    :

    15px

    ;
  55. font-size

    :

    20px

    ;
  56. margin-bottom

    :

    10px

    ;
  57. color

    :

    #FFF

    ;
  58. font-family

    :

    'Tahoma'
  59. }

  60. label:

    before

    {
  61. content

    :

    ""

    ;
  62. display

    :

    inline-block

    ;
  63. width

    :

    20px

    ;
  64. height

    :

    20px

    ;
  65. margin-right

    :

    10px

    ;
  66. position

    :

    absolute

    ;
  67. left

    :

    0

    ;
  68. bottom

    :

    1px

    ;
  69. background-color

    :

    cadetblue

    ;
  70. box-shadow

    :

    inset

    0px

    2px

    3px

    0px

    rgba

    (

    0

    ,

    0

    ,

    0

    ,

    .3)

    ,

    0px

    1px

    0px

    0px

    rgba

    (

    255

    ,

    255

    ,

    255

    ,

    .8)

    ;
  71. border-radius

    :

    3px

    ;
  72. }
  73. input[

    type=

    checkbox

    ]

    {
  74. display

    :

    none

    ;
  75. }
  76. input[

    type=

    checkbox

    ]

    :

    checked

    +

    label:

    before

    {
  77. content

    :

    "\2713

    "

    ;
  78. text-shadow

    :

    1px

    1px

    1px

    rgba

    (

    0

    ,

    0

    ,

    0

    ,

    .2)

    ;
  79. font-size

    :

    20px

    ;
  80. color

    :

    #FFF

    ;
  81. text-align

    :

    center

    ;
  82. line-height

    :

    15px

    ;
  83. }


  84. #demo-header

    {
  85. height

    :

    150px

    ;
  86. width

    :

    100%

    ;
  87. background

    :

    url

    (

    'http://www.jqueryajaxphp.com/wp-content/uploads/2015/01/JAP-Logo.png'

    )

    center

    no-repeat

    !important;
  88. margin-bottom

    :

    25px

    ;
  89. border-bottom

    :

    1px

    solid

    #999

    ;
  90. padding-bottom

    :

    25px

    ;
  91. }

  92. footer {
  93. width

    :

    786px

    ;
  94. font

    :

    normal

    16px

    Arial,

    Helvetica,

    sans-serif

    ;
  95. padding

    :

    15px

    35px

    ;
  96. position

    :

    fixed

    ;
  97. bottom

    :

    0

    ;
  98. left

    :

    50%

    ;
  99. margin-left

    :

    -420px

    ;
  100. background-color

    :

    #1f1f1f

    ;
  101. background-image

    :

    -webkit-linear-gradient(

    top

    ,

    #1f1f1f

    ,

    #101010

    )

    ;
  102. background-image

    :

    -moz-linear-gradient(

    top

    ,

    #1f1f1f

    ,

    #101010

    )

    ;
  103. background-image

    :

    linear-gradient(

    top

    ,

    #1f1f1f

    ,

    #101010

    )

    ;
  104. border-radius

    :

    2px

    2px

    0

    0

    ;
  105. box-shadow

    :

    0

    -1px

    4px

    rgba

    (

    0

    ,

    0

    ,

    0

    ,

    0.4

    )

    ;
  106. z-index

    :

    1

    ;
  107. text-align

    :

    center

    ;
  108. }

  109. a {
  110. color

    :

    #FFFFFF

    ;
  111. }

Our Goal in this tutorial is to create a page that has multiple data in listed and different checkboxes options for filtering the data. In my case, I will use some sample images in my list. You can remove the "img" tags or change the source attribute of the images when copying/pasting the codes that will be provided below.

Creating Markup

Create an "index.html" file and copy/paste the codes below. Please don't forget to load the jQuery library and the CSS file in your markup

Example

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    name

    =

    "viewport"

    content

    =

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

    >
  6. <title

    >

    Live Filter with Multiple Options using jQuery</

    title

    >

  7. <link

    rel

    =

    "stylesheet"

    href

    =

    "assets/style.css"

    >
  8. <script

    src

    =

    "assets/jquery-1.10.2.js"

    ></

    script

    >
  9. </

    head

    >
  10. <body

    >
  11. <!-- Place the markup scripts here -->
  12. </

    body

    >
  13. </

    html

    >

This is a simple HTML source code that contains three checkboxes used to filter the data.

  1. <div

    id

    =

    "filters"

    >
  2. <div

    class

    =

    "filterblock"

    >
  3. <input

    id

    =

    "check1"

    type

    =

    "checkbox"

    name

    =

    "check"

    value

    =

    "php"

    class

    =

    "category"

    >
  4. <label

    for

    =

    "check1"

    >

    PHP</

    label

    >
  5. </

    div

    >

  6. <div

    class

    =

    "filterblock"

    >
  7. <input

    id

    =

    "check2"

    type

    =

    "checkbox"

    name

    =

    "check"

    value

    =

    "visual_basic"

    class

    =

    "category"

    >
  8. <label

    for

    =

    "check2"

    >

    Visual Basic</

    label

    >
  9. </

    div

    >

  10. <div

    class

    =

    "filterblock"

    >
  11. <input

    id

    =

    "check3"

    type

    =

    "checkbox"

    name

    =

    "check"

    value

    =

    "android"

    class

    =

    "category"

    >
  12. <label

    for

    =

    "check3"

    >

    Android</

    label

    >
  13. </

    div

    >
  14. </

    div

    >

Next is we will create some sample data to be displayed. We will use "resultblock" as the class name for all sets of data and each set will have a custom data attribute which is the "data-tag" for the identification of each set when filtering the list.

  1. <div

    class

    =

    "searchresults"

    >

  2. <div

    class

    =

    "resultblock"

    data-tag=

    "php"

    >
  3. <img

    src

    =

    "img/php.jpg"

    class

    =

    "itemimg"

    >
  4. <div

    class

    =

    "desc"

    >
  5. <div

    class

    =

    "desc_text"

    >
  6. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  7. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  8. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  9. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  10. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  11. </

    div

    >
  12. </

    div

    >
  13. </

    div

    >

  14. <div

    class

    =

    "resultblock"

    data-tag=

    "android"

    >
  15. <img

    src

    =

    "img/android.png"

    class

    =

    "itemimg"

    >
  16. <div

    class

    =

    "desc"

    >
  17. <div

    class

    =

    "desc_text"

    >
  18. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  19. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  20. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  21. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  22. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  23. </

    div

    >
  24. </

    div

    >
  25. </

    div

    >

  26. <div

    class

    =

    "resultblock"

    data-tag=

    "visual_basic"

    >
  27. <img

    src

    =

    "img/vb.png"

    class

    =

    "itemimg"

    >
  28. <div

    class

    =

    "desc"

    >
  29. <div

    class

    =

    "desc_text"

    >
  30. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  31. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  32. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  33. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  34. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  35. </

    div

    >
  36. </

    div

    >
  37. </

    div

    >

  38. <div

    class

    =

    "resultblock"

    data-tag=

    "php"

    >
  39. <img

    src

    =

    "img/php1.png"

    class

    =

    "itemimg"

    >
  40. <div

    class

    =

    "desc"

    >
  41. <div

    class

    =

    "desc_text"

    >
  42. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  43. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  44. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  45. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  46. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  47. </

    div

    >
  48. </

    div

    >
  49. </

    div

    >

  50. <div

    class

    =

    "resultblock"

    data-tag=

    "visual_basic"

    >
  51. <img

    src

    =

    "img/vb2.jpg"

    class

    =

    "itemimg"

    >
  52. <div

    class

    =

    "desc"

    >
  53. <div

    class

    =

    "desc_text"

    >
  54. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  55. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  56. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  57. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  58. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  59. </

    div

    >
  60. </

    div

    >
  61. </

    div

    >

  62. <div

    class

    =

    "resultblock"

    data-tag=

    "android"

    >
  63. <img

    src

    =

    "img/android2.jpg"

    class

    =

    "itemimg"

    >
  64. <div

    class

    =

    "desc"

    >
  65. <div

    class

    =

    "desc_text"

    >
  66. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  67. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  68. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  69. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  70. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  71. </

    div

    >
  72. </

    div

    >
  73. </

    div

    >

  74. <div

    class

    =

    "resultblock"

    data-tag=

    "visual_basic"

    >
  75. <img

    src

    =

    "img/vb1.png"

    class

    =

    "itemimg"

    >
  76. <div

    class

    =

    "desc"

    >
  77. <div

    class

    =

    "desc_text"

    >
  78. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  79. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  80. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  81. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  82. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  83. </

    div

    >
  84. </

    div

    >
  85. </

    div

    >

  86. <div

    class

    =

    "resultblock"

    data-tag=

    "php"

    >
  87. <img

    src

    =

    "img/php2.png"

    class

    =

    "itemimg"

    >
  88. <div

    class

    =

    "desc"

    >
  89. <div

    class

    =

    "desc_text"

    >
  90. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  91. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  92. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  93. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  94. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  95. </

    div

    >
  96. </

    div

    >
  97. </

    div

    >

  98. <div

    class

    =

    "resultblock"

    data-tag=

    "android"

    >
  99. <img

    src

    =

    "img/android1.jpg"

    class

    =

    "itemimg"

    >
  100. <div

    class

    =

    "desc"

    >
  101. <div

    class

    =

    "desc_text"

    >
  102. Nulla et dapibus odio. Ut vestibulum turpis neque, at accumsan lacus luctus elementum.
  103. Nulla et volutpat ante, efficitur tempor arcu. Ut eget scelerisque massa. Nam sed mattis
  104. sapien, ac congue ex. Cras cursus nisi vitae magna molestie, a scelerisque tellus dapibus.
  105. Integer vitae condimentum ex. Integer congue orci aliquet purus aliquet gravida. Nulla gravida
  106. vulputate interdum. Aenean elementum dolor in dui tincidunt pharetra.
  107. </

    div

    >
  108. </

    div

    >
  109. </

    div

    >

  110. </

    div

    >

jQuery Script for Filtering

The below code is the script that will filter our list when the checkboxes' properties will change.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '.category'

    )

    .on

    (

    'change'

    ,

    function

    (

    )

    {
  3. var

    category_list =

    [

    ]

    ;
  4. $(

    '#filters :input:checked'

    )

    .each

    (

    function

    (

    )

    {
  5. var

    category =

    $(

    this

    )

    .val

    (

    )

    ;
  6. category_list.push

    (

    category)

    ;
  7. }

    )

    ;

  8. if

    (

    category_list.length

    ==

    0

    )
  9. $(

    '.resultblock'

    )

    .fadeIn

    (

    )

    ;
  10. else

    {
  11. $(

    '.resultblock'

    )

    .each

    (

    function

    (

    )

    {
  12. var

    item =

    $(

    this

    )

    .attr

    (

    'data-tag'

    )

    ;
  13. if

    (

    jQuery.inArray

    (

    item,

    category_list)

    >

    -

    1

    )
  14. $(

    this

    )

    .fadeIn

    (

    'slow'

    )

    ;
  15. else
  16. $(

    this

    )

    .hide

    (

    )

    ;
  17. }

    )

    ;
  18. }
  19. }

    )

    ;
  20. }

    )

    ;

Output

For the PHP value

php-filter_0.jpg


For the Visual Basic value

vb-filter.jpg


For the Android value

android-filter.jpg


DEMO

⚙ Live Demo

Learn by Examples

Examples are better than thousands of words. Examples are often easier to understand than text explanations. For the full source code, kindly click the "Download Code" button below.

I hope that this simple yet useful tutorial that I created may help you with your future projects.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected].

Practice Coding.

Thank you very much.


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

452,496

332,845

332,853

Top