Home
Tutorial
Todo list
Search Feature in Todo List
Online HTML Editor : Search in todo list
Run Output
Output Rosolution:
<!DOCTYPE html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- bootstrap 5.0.2 css for design --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <!-- font awesome css for icons --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <div class="container-fluid"> <div class="container"> <div class="mx-auto mt-5" style="max-width: 700px;"> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Enter Your Name" aria-label="Recipient's username" id="username" aria-describedby="button-addon2"> <button class="btn text-white btn-info" type="button" id="addUser">Add User</button> </div> <div class="filter my-3"> <div class="row justify-content-between"> <div class="col"> </div> <div class="col"> Search: <div class="input-group"> <input type="search" class="form-control" placeholder="Search..." aria-label="Recipient's username" id="search" > </div> </div> </div> </div> <div class="display"> <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Your Name</th> <th scope="col">Actions</th> </tr> </thead> <tbody id="records"> <tr> <th scope="row">1</th> <td>King of Javascript</td> <td><i class="btn text-white fa fa-edit btn-info mx-2"></i> <i class="btn btn-danger text-white fa fa-trash"></i></td> </tr> </tbody> </table> </div> </div> </div> </div> <script> const addUserBtn = document.getElementById('addUser'); const btnText = addUserBtn.innerText; const usernameTextField = document.getElementById('username'); const recordsDisplay = document.getElementById('records'); let userArray = []; let edit_id = null; let allTr; let objStr = localStorage.getItem('users'); if (objStr != null) { userArray = JSON.parse(objStr); } DisplayInfo(); addUserBtn.onclick = () => { //get user's name from text field const name = usernameTextField.value; if (edit_id != null) { //edit action userArray.splice(edit_id, 1, { 'name': name }); edit_id = null; } else { //insert action userArray.push({ 'name': name }); } SaveInfo(userArray); usernameTextField.value = ''; addUserBtn.innerText = btnText; } // store user's name in local storage function SaveInfo(userArray) { let str = JSON.stringify(userArray); localStorage.setItem('users', str); DisplayInfo(); } // display user's name function DisplayInfo() { let statement = ''; userArray.forEach((user, i) => { statement += `<tr> <th scope="row">${i+1}</th> <td>${user.name}</td> <td><i class="btn text-white fa fa-edit btn-info mx-2" onclick='EditInfo(${i})'></i> <i class="btn btn-danger text-white fa fa-trash" onclick='DeleteInfo(${i})'></i></td> </tr>`; }); recordsDisplay.innerHTML = statement; //select all tr of table allTr = document.querySelectorAll('#records tr'); } // edit user's name function EditInfo(id) { edit_id = id; usernameTextField.value = userArray[id].name; addUserBtn.innerText = 'Save Changes'; } //delete user's name function DeleteInfo(id) { userArray.splice(id, 1); SaveInfo(userArray); } //search feature code //get text as query from search text field const searchInputField = document.querySelector('#search'); searchInputField.addEventListener('input', function (e) { const searchStr = e.target.value.toLowerCase(); recordsDisplay.innerHTML = ''; allTr.forEach(tr => { const td_in_tr = tr.querySelectorAll('td'); if (td_in_tr[0].innerText.toLowerCase().indexOf(searchStr) > -1) { recordsDisplay.appendChild(tr); } }); if (recordsDisplay.innerHTML == '') { recordsDisplay.innerHTML = ' No Records Found'; } }); </script> </body> </html>