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

Live Search Using JavaScript Source Code

ollisykes19

Crypto Transaction Analyst
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
37
Likes
42
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 600 XP
In this tutorial we will create a Live Search using JavaScript. This code will search a specific keyword when the user enter some keyword in the textbox. The code use onkeyup() function to call a specific function that automatically search the data when entered, by the use of for() loop and adding an if statement with a sets of parameter indexOf() in order to filter the array of data. Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and 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"

    >

    Live Search Using JavaScript Source Code</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-8"

    >
  18. <table

    class

    =

    "table table-bordered"

    >
  19. <thead

    class

    =

    "alert-info"

    >
  20. <tr

    >
  21. <th

    >

    Full Name</

    th

    >
  22. </

    tr

    >
  23. </

    thead

    >
  24. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  25. </

    table

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-md-4"

    >
  28. <div

    class

    =

    "form-group"

    >
  29. <label

    >

    Enter a name</

    label

    >
  30. <div

    class

    =

    "input-group"

    >
  31. <div

    class

    =

    "input-group-addon"

    ><span

    class

    =

    "glyphicon glyphicon-search"

    ></

    span

    ></

    div

    >
  32. <input

    type

    =

    "text"

    id

    =

    "search"

    placeholder=

    "Search here..."

    onkeyup

    =

    "autoSearch()"

    class

    =

    "form-control"

    /

    >
  33. </

    div

    >
  34. </

    div

    >
  35. </

    div

    >
  36. </

    div

    >
  37. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  38. </

    body

    >
  39. </

    html

    >

Creating the Script

This code contains the script of the application. This code will live search a data when a keyword is entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var

    members =

    [

    "John Smith"

    ,

    "Claire Temple"

    ,

    "Sarah Goat"

    ]

    ;

  2. displayList(

    )

    ;


  3. function

    autoSearch(

    )

    {
  4. var

    search=

    document.getElementById

    (

    'search'

    )

    .value

    .toLowerCase

    (

    )

    ;
  5. var

    parent=

    document.getElementById

    (

    "result"

    )

    ;
  6. var

    children=

    parent.getElementsByTagName

    (

    'tr'

    )

    ;

  7. for

    (

    var

    i=

    0

    ;

    i<

    children.length

    ;

    i++

    )

    {
  8. var

    keyword=

    children[

    i]

    .innerText

    ;
  9. if

    (

    keyword.toLowerCase

    (

    )

    .indexOf

    (

    search)

    >-

    1

    )

    {
  10. children[

    i]

    .style

    .display

    =

    ""

    ;
  11. }

    else

    {
  12. children[

    i]

    .style

    .display

    =

    "none"

    ;
  13. }
  14. }

  15. }


  16. function

    displayList(

    )

    {
  17. members.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 <

    members.length

    ;

    i++

    )

    {
  28. data +=

    "<tr><td>"

    +

    members[

    i]

    +

    "</td></tr>"

    ;
  29. }

  30. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  31. }

There you have it we successfully created a Live Search using 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!


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

449,193

322,229

322,238

Top