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

How to Create Filter Search List in JavaScript

f0xayy

Follower Engagement Pro
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
64
Likes
80
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Create Filter Search List in JavaScript

Introduction

In this tutorial we will create a How to Create Filter Search List in JavaScript. This tutorial purpose is to enable you to search a list with the filter function. This will cover all the basic function that will search a list. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system that needed to to filter any important data. I will give my best to provide you the easiest way of creating this program Filter Search List. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only list and the search box. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

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

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Create Filter Search List in JavaScript</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <div

    class

    =

    "input-group"

    >
  20. <div

    class

    =

    "input-group-addon"

    ><span

    class

    =

    "glyphicon glyphicon-search"

    ></

    span

    ></

    div

    >
  21. <input

    type

    =

    "text"

    id

    =

    "search"

    placeholder

    =

    "Search here..."

    onkeyup

    =

    "searchResult()"

    class

    =

    "form-control"

    /

    >
  22. </

    div

    >
  23. </

    div

    >
  24. <ul

    class

    =

    "list-group"

    id

    =

    "list"

    ></

    ul

    >
  25. </

    div

    >
  26. </

    div

    >
  27. <script

    src

    =

    "script.js"

    ></

    script

    >
  28. </

    body

    >
  29. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will try to filter the entered result in the list. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let name_list =

    [

    'Steve'

    ,

    'Bruce'

    ,

    'Tony'

    ,

    'Rimuru'

    ,

    'Millim'

    ,

    'Leon'

    ,

    'Takamitchy'

    ,

    'Draken'

    ]

    ;

  2. displayList(

    )

    ;


  3. function

    searchResult(

    )

    {
  4. let search =

    document.getElementById

    (

    'search'

    )

    .value

    .toLowerCase

    (

    )

    ;
  5. let target =

    document.getElementById

    (

    "list"

    )

    ;
  6. let list =

    target.getElementsByTagName

    (

    'li'

    )

    ;
  7. for

    (

    var

    i=

    0

    ;

    i <

    list.length

    ;

    i++

    )

    {
  8. let text =

    list[

    i]

    .innerHTML

    ;
  9. if

    (

    text.toLowerCase

    (

    )

    .indexOf

    (

    search)

    >

    -

    1

    )

    {
  10. list[

    i]

    .style

    .display

    =

    ""

    ;
  11. }

    else

    {
  12. list[

    i]

    .style

    .display

    =

    "none"

    ;
  13. }
  14. }
  15. }

  16. function

    displayList(

    )

    {
  17. name_list.sort

    (

    function

    (

    a,

    b)

    {
  18. if

    (

    a <

    b)

    {
  19. return

    -

    1

    ;
  20. }

  21. if

    (

    a >

    b)

    {
  22. return

    1

    ;
  23. }

  24. return

    0

    ;
  25. }

    )

    ;

  26. data =

    ""

    ;

  27. for

    (

    var

    i=

    0

    ;

    i <

    name_list.length

    ;

    i++

    )

    {
  28. data +=

    "<li class='list-group-item'><a href='https://www.google.com/search?q="

    +

    name_list[

    i]

    +

    "'>"

    +

    name_list[

    i]

    +

    "</a></li>"

    ;
  29. }

  30. document.getElementById

    (

    'list'

    )

    .innerHTML

    =

    data;
  31. }

In the code above we just simply first create a variable that will handle the array of the list. Then we create a new method called displayList(), this function will sort out the filtered data that we enteredfrom our displayed list. And last we will create a method called searchResult(). This function will bind the search box and will display a result base on the entered keyword.

Output:

How%20to%20Create%20Filter%20Search%20List%20in%20JavaScript%201.png


The How to Create Filter Search List in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Filter Search List in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials


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

452,292

323,517

323,526

Top