Introduction
In this project, you will learn how to use JavaScript to implement a simple search functionality. You will learn how to bind the input event to an input box, process the search data, and display the search results.
👀 Preview

🎯 Tasks
In this project, you will learn:
- How to bind the
inputevent to an input box - How to implement the
handleInputmethod to filter the search data - How to render the search results in a list
🏆 Achievements
After completing this project, you will be able to:
- Use the
addEventListenermethod to bind events - Use the
filtermethod to filter an array based on a condition - Manipulate the DOM to update the search results
Set Up the Project Structure
In this step, you will set up the project files and structure. Follow the steps below to complete this step:
Open the project folder. The directory structure is as follows:
├── css
│ └── style.css
├── index.html
└── js
└── index.js
Where:
index.htmlis the main page.cssis the folder for storing the project styles.js/index.jsis the JavaScript file where you need to add code.
Click on Go Live button in the bottom right corner of WebIDE, to run the project.
Next, open "Web 8080" on the top of the VM and refresh it manually and you will see the page.
Render the Search Results
In this step, you will implement the render method to display the search results.
- In the
js/index.jsfile, locate therendermethod in thesearchobject. - Inside the
rendermethod, add the following code to generate the HTML template for the search results and update thelistElelement:
const template = this.data.reduce(
(prev, next) => prev + `<li>${next.content}</li>`,
""
);
this.listEl.innerHTML = template;
The reduce method is used to create an HTML template string that displays the content property of each item in the data array.
Implement the handleInput Method
In this step, you will implement the handleInput method to process the search functionality.
- In the
js/index.jsfile, locate thehandleInputmethod in thesearchobject. - Inside the
handleInputmethod, add the following code to filter thedataarray based on the input value and then render the search results:
const value = e.target.value;
// Use timer to simulate ajax send request, data receive data
setTimeout(() => {
this.data = !!value
? data.filter((val) => val.content.indexOf(value) !== -1)
: [];
this.render();
});
The setTimeout function is used to simulate an asynchronous operation, such as an AJAX request, to receive the search data.
Bind the Input Event
In this step, you will learn how to bind the input event to the input box and call the handleInput method when the value of the input box changes.
- Open the
js/index.jsfile. - Locate the
handlefunction in thesearchobject. - Inside the
handlefunction, add the following code to bind theinputevent to theinputElelement and call thehandleInputmethod:
this.inputEl.addEventListener("input", this.handleInput.bind(this));
The bind(this) part is important to ensure that the this inside the handleInput method refers to the search object itself.
After completing these four steps, the js/index.js file should now have the complete implementation of the search functionality, and the project should work as expected. The final effect should be as follows:

Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



