As per the fiddle link mentioned in the question
The piece of code should also be present in the event triggered when deleting and adding the contact.
Example deleting contact should have this statement inside it
function deleteContact(e) {
if (e.target.classList.contains("delete")) {
if (confirm("Are you sure?")) {
e.target.parentElement.remove();
}
} else if (e.target.classList.contains("fas")) {
if (confirm("Are you sure?")) {
e.target.parentElement.parentElement.remove();
}
}
if (contactList.children.length < 1) {
noContactsMessage.style.display = "block";
}
// NO CONTACTS MESSAGE IF THERE ARE NO CONTACTS
}
and Add function should look like this
function addContact(e) {
e.preventDefault();
const nameValue = addForm.querySelector("#name-input").value;
const numberValue = addForm.querySelector("#number-input").value;
const li = document.createElement("li");
const nameSpan = document.createElement("span");
nameSpan.classList.add("name");
nameSpan.textContent = nameValue;
const numberSpan = document.createElement("span");
numberSpan.classList.add("number");
numberSpan.textContent = numberValue;
const deleteButton = document.createElement("span");
deleteButton.classList.add("delete");
const delIcon = document.createElement("i");
delIcon.className = "fas fa-user-minus";
deleteButton.appendChild(delIcon);
li.appendChild(nameSpan);
li.appendChild(numberSpan);
li.appendChild(deleteButton);
contactList.appendChild(li);
if (contactList.children.length >= 1) {
noContactsMessage.style.display = "none";
}
}
your logic is not getting called in the event that is the reason the code was not working as expected.