JavaScript - Auto Find Data Base On Keyword
Submitted by razormist on Thursday, January 23, 2020 - 09:20.
In this tutorial we will create a Auto Find Data Base On Keyword using JavaScript. This code will auto find a specific data every time the user enter some keyword in the textbox. The code use onkeyup() function to call a specific function that automatically find a certain data when keyword is entered, by the use of for() loop and adding an if statement with an argument of indexOf() it can filter the index position of an array automatically. 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.
There you have it we successfully created a Auto Find Data Base On Keyword 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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- </tr>
- </thead>
- </table>
- </div>
- <div class="col-md-4">
- <div class="form-group">
- <div class="input-group">
- <input type="text" id="search" placeholder="Search here..." onkeyup="autoFind()" class="form-control"/>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will auto find a certain data when 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.- var cars = ['Toyota', 'Honda', 'Mustang', 'Acura', 'Audi', 'Bugatti'];
- function autoFind(){
- var search=document.getElementById('search').value.toLowerCase();
- var parent=document.getElementById("result");
- var children=parent.getElementsByTagName('tr');
- for(var i=0; i<children.length; i++){
- var keyword=children[i].innerText;
- if(keyword.toLowerCase().indexOf(search)>-1){
- children[i].style.display="";
- }else{
- children[i].style.display = "none";
- }
- }
- }
- function displayList(){
- cars.sort(function(a,b){
- if(a < b){
- return -1;
- }
- if(a > b){
- return 1;
- }
- return 0;
- });
- data = "";
- for(var i=0; i < cars.length; i++){
- data += "<tr><td>"+cars[i]+"</td></tr>";
- }
- document.getElementById('result').innerHTML = data;
- }
- displayList();
Add new comment
- 78 views