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

Auto Search Using JavaScript

Jonny

Fantasy World Explorer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
118
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this code we will tackle about Auto Search using JavaScript. The program will enable to automatically search a row data. You are free to modify, and use this code. To learn more about this, just follow the steps below.

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 this 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"

    >

    Auto Search Using JavaScript</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Enter a name</

    label

    >
  20. <div

    class

    =

    "input-group"

    >
  21. <div

    class

    =

    "input-group-addon"

    ><span

    class

    =

    "glyphicon glyphicon-search"

    ></

    span

    ></

    div

    >
  22. <input

    type

    =

    "text"

    id

    =

    "search"

    placeholder=

    "Search here..."

    onkeyup

    =

    "autoSearch()"

    class

    =

    "form-control"

    /

    >
  23. </

    div

    >
  24. </

    div

    >
  25. </

    div

    >

  26. <table

    class

    =

    "table table-bordered"

    >
  27. <thead

    class

    =

    "alert-info"

    >
  28. <tr

    >
  29. <th

    >

    Member List</

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  33. </

    table

    >
  34. <ul

    class

    =

    "list-group"

    id

    =

    "list"

    ></

    ul

    >

  35. </

    div

    >
  36. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

    html

    >

Creating the Script

This code contains the script of the application. The code will automatically search a specific keyword. 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 =

    [

    'Tony Stark'

    ,

    'Jin Mori'

    ,

    'Luke Cage'

    ]

    ;

  2. displayMember(

    )

    ;

  3. function

    displayMember(

    )

    {
  4. members.sort

    (

    function

    (

    a,

    b)

    {
  5. if

    (

    a <

    b)

    {
  6. return

    -

    1

    ;
  7. }

  8. if

    (

    a >

    b)

    {
  9. return

    1

    ;
  10. }

  11. return

    0

    ;
  12. }

    )

    ;

  13. data =

    ""

    ;

  14. for

    (

    var

    i=

    0

    ;

    i <

    members.length

    ;

    i++

    )

    {
  15. data +=

    "<tr><td>"

    +

    members[

    i]

    +

    "</td></tr>"

    ;
  16. }

  17. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  18. }

  19. function

    autoSearch(

    )

    {
  20. var

    search=

    document.getElementById

    (

    'search'

    )

    .value

    .toLowerCase

    (

    )

    ;
  21. var

    parent=

    document.getElementById

    (

    "result"

    )

    ;
  22. var

    children=

    parent.getElementsByTagName

    (

    'tr'

    )

    ;

  23. for

    (

    var

    i=

    0

    ;

    i<

    children.length

    ;

    i++

    )

    {
  24. var

    keyword=

    children[

    i]

    .innerText

    ;
  25. if

    (

    keyword.toLowerCase

    (

    )

    .indexOf

    (

    search)

    >-

    1

    )

    {
  26. children[

    i]

    .style

    .display

    =

    ""

    ;
  27. }

    else

    {
  28. children[

    i]

    .style

    .display

    =

    "none"

    ;
  29. }
  30. }

  31. }

There you have it we successfully created a Auto 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 the hidden content.
 

452,496

338,535

338,543

Top