I was creating a javascript item lister app, and i wanted to add delete item functionality. I am able to delete dummy items but the newly created items won’t delete.
Here’s the codepen: https://codepen.io/saabk/pen/eYGWarr
Here’s the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>DOM</title>
</head>
<body>
<header id="main-header" class="bg-success text-white p-4 mb-3">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1 id="header-title">Item Lister</h1>
</div>
<div class="col-md-6 align-self-center">
<input type="text" class="form-control" id="filter" placeholder="Search items...">
</div>
</div>
</div>
</header>
<div class="container my-4">
<div class="card card-body" id="main">
<h2 class="title">Add Items</h2>
<form id="add-form" class="form-inline mb-3">
<input id="inputItem" type="text" class="form-control mr-2">
<input type="submit" class="btn btn-dark my-2" value="Submit">
</form>
<h2 class="title">Items</h2>
<ul id="items" class="list-group">
<li class="list-group-item">Item 1<button style="float: right;" class="btn btn-danger btn-sm delete">X</button>
</li>
<li class="list-group-item">Item 2<button style="float: right;" class="btn btn-danger btn-sm delete">X</button>
</li>
<li class="list-group-item">Item 3<button style="float: right;" class="btn btn-danger btn-sm delete">X</button>
</li>
<li class="list-group-item">Item 4<button style="float: right;" class="btn btn-danger btn-sm delete">X</button>
</li>
</ul>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
Here’s javascript code:
let form = document.querySelector('#add-form');
let itemsList = document.querySelector('#items');
let deleteButton = document.querySelectorAll('.delete');
form.addEventListener('submit', addItem);
deleteButton.forEach((item) => {
item.addEventListener('click', deleteItem)
})
function addItem(e) {
e.preventDefault();
let newItem = document.querySelector('#inputItem');
let itemText = document.createTextNode(newItem.value);
let li = document.createElement('li');
li.className = 'list-group-item';
let delBtn = document.createElement('button');
delBtn.className = 'btn btn-danger btn-sm delete';
delBtn.textContent = 'X'
delBtn.style.float = 'right';
li.appendChild(itemText);
li.appendChild(delBtn);
itemsList.appendChild(li);
}
function deleteItem(e) {
if(e.target.classList.contains('delete')) {
if(confirm('Are you sure about that?')) {
let li = e.target.parentElement;
itemsList.removeChild(li);
}
}
}
The dummy items can be deleted but items created by the user won’t delete. Also i am using bootsrap. Please tell me how can i fix this issue.
Thank You!