when-i-click-a-button-my-style-dosent-work-at-all

by jacoballen4534

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TODO List</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <div class="header">
        <h2 class="title">todo list</h2>
        <div class="input-container">
            <input type="text" placeholder="//Enter your list ..." id="input-item">
            <button class="add-btn">add item</button>
        </div>
    </div>

    <ul class="list-container">

    </ul>

</body>
<script src="./app.js"></script>
</html>

CSS

/* css rest */
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body , html{
    font-family: Arial, Helvetica, sans-serif;
}

body{
    width: 100%;
    background-color: #95a5a6;
}
/* todo style */
.header{
    margin: 200px auto 0px;
    width: min(80% , 400px);
    text-align: center;
}

h2{
    font-weight: bold;
    font-size: 2em;
    text-transform: uppercase;
    color: #2c3e50;
}

.input-container{
    margin-top: 20px;
    display: flex;
    justify-content: center;
    gap: 10px;
}

.add-btn{
    cursor: pointer;
    display: inline;
    font-size: 1.1em;
    background-color: #34495e;
    color: #fff;
    text-transform: capitalize;
    border: 1px solid #2c3e50;
    padding: .4em .8em;
    transition: background .3s ;
}

.add-btn:hover{
    background-color: #fff;
    color: #2c3e50;
}

#input-item{
    width: 70%;
    font-size: 1.1em;
    padding: .2em .4em;
    outline: 2px solid rgb(165, 165, 165);
    transition: background .3s ;
}

#input-item:focus{
    background-color: #5a7086;
    border: 1px solid #95a5a6;
    color: #fff;
    outline: none;
}

#input-item:focus::placeholder{
    color: #fff;
}

.list-container{
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.list{
    cursor: pointer;
    position: relative;
    display: flex;
    width: 300px;
    justify-content: space-between;
    align-items: center;
    overflow: hidden;
    padding: .3em .4em;
    transform: background .2s ;
}

.list p{
    color: #2c3e50;
    text-transform: capitalize;
    font-weight: bold;
    padding-left: 30px;
    transform: translateX(-30px);
    transition: transform .2s;
}

.list button{
    background: none;
    border: none;
}

.list:hover p{
    transform: translateX(0px);
}

.list.selected{
    background-color: #1dd1a1 !important;
}

.list.selected p{
    transform: translateX(0px);
}

.list p::before{
    content: '';
    position: absolute;
   ...

JavaScript

const inputEl = document.getElementById("input-item");
const addBtn = document.querySelector(".add-btn");

let lists = [];

const renderList = () => {
  const listContainer = document.querySelector(".list-container");
  listContainer.innerHTML = "";

  lists.forEach((item) => {
    const liContainer = document.createElement("li");
    liContainer.setAttribute("id", item.id);
    liContainer.classList.add("list", item.selected ? "selceted" : "not");

    const listName = document.createElement("p");
    listName.innerText = item.name;
    listName.addEventListener("click", () => markSelected(item.id));

    const iconBtn = document.createElement("img");
    iconBtn.setAttribute("src", "/icons/close-square-svgrepo-com.svg");
    iconBtn.addEventListener("click", () => deletList(item.id));

    liContainer.appendChild(listName);
    liContainer.appendChild(iconBtn);
    listContainer.appendChild(liContainer);
  });
};

const addList = () => {
  const inputVal = inputEl.value;
  let list;

  if (inputVal.trim() !== "") {
    list = {
      id: Date.now(),
      name: inputVal,
      selected: false,
    };
  }

  inputEl.value = "";
  lists.push(list);
  renderList();

  console.log(lists);
};

addBtn.addEventListener("click", (e) => {
  addList();
});

const deletList = (id) => {
  lists = lists.filter((list) => list.id !== id);
  renderList();
};

const markSelected = (id) => {
  lists = lists.map((list) => {
    if (list.id === id) {
      list.selected = !list.selected;
    }

    return list;
  });
  updateList(id);
};

const updateList = (id) => {
  const listElement = document.getElementById(id);
  if (listElement) {
    const task = lists.find((item) => item.id === id);
    if (task.selected) {
      listElement.classList.add("selected");
    } else {
      listElement.classList.add("selected");
    }
  }
};

renderList();