How to Close List Items with JavaScript? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Closing list items dynamically with JavaScript involves creating a user interface where each list item has a button or an icon that allows the user to remove that particular item from the list. Basically this is commonly seen in task in task management applications shopping lists and other similar UIs where Items can be added and removed by the user. In this article, we explain about how to add and remove items from the list dynamically. Below we provide that information for reference with related examples and outputs.Output PreviewOutput PreviewPrerequisitesHTMLCSSJavaScriptBootstrap FrameworkThese are the approaches to close list items with JavaScriptFirst we create a basic User Interface for adding Items to the list by using JavaScript and Bootstrap. After this on click on add item button we added one list of item to item dynamically.In same way if you want to remove any item from the list then click on X symbol of a particular item it will be removed from the List of items dynamically. Once Development is completed. Now this HTML file through any browser then you can the output.ExampleBelow is a example demonstrating adding Items to the list by using JavaScript and Bootstrap.In which when you click on add item button we added one list of item to item dynamically. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href=" https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"> </script> <title>Close List Items</title> <style> ul { list-style-type: none; padding: 0; margin: 0; } li { margin: 5px 0; padding: 10px; background-color: #f9f9f9; border: 1px solid #ddd; position: relative; font-weight: 700; } .close { position: absolute; right: 10px; top: 10px; cursor: pointer; font-size: 18px; } </style> </head> <body> <div class="container mt-5 p-5 bg-success" style="max-width: 1000px;"> <h3 class="mb-4 mt-2 text-center text-light">List of Items</h3> <ul id="myList"> </ul> <button id="addItem" class="btn btn-light mt-3" style="font-weight: bold;">Add Item</button> </div> <script> // Function to attach close functionality to all close buttons function attachCloseFunctionality() { const closeButtons = document.querySelectorAll('.close'); closeButtons.forEach(button => { button.addEventListener('click', function () { const listItem = this.parentElement; listItem.remove(); // Remove the list item from the DOM }); }); } // Function to add a new list item function addNewItem(text) { const li = document.createElement('li'); li.textContent = text; const span = document.createElement('span'); span.textContent = '\u00D7'; // Unicode for '×' span.className = 'close'; li.appendChild(span); document.getElementById('myList').appendChild(li); // Attach event listener to the new close button span.addEventListener('click', function () { li.remove(); // Remove the list item from the DOM }); } // Initial call to attach close functionality attachCloseFunctionality(); // Event listener for adding a new item document.getElementById('addItem').addEventListener ('click', function () { addNewItem('Item'); }); </script> </body> </html> Output:ConclusionDynamically closing list items in JavaScript can be implemented using various approaches depending on the complexity of the application and the need for maintainability. For simple tasks, using inline JavaScript with event listeners or event delegation is sufficient. For larger applications leveraging a framework like React provides better state management and scalability. Create Quiz Comment M myartic07g9 Follow 1 Improve M myartic07g9 Follow 1 Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like