Загрузка данных


<!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="UTF-8" />
  <title>Список товаров</title>
  <link rel="stylesheet" href="style.css" />
</head>
<body>
  <div class="container">
    <h1>Список товаров</h1>

    <form id="add-form">
      <input type="text" id="item-name" placeholder="Название товара" required />
      <input type="text" id="item-desc" placeholder="Описание" />
      <button type="submit">Добавить</button>
    </form>

    <ul id="items-list"></ul>
  </div>

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





body {
  font-family: Arial, sans-serif;
  margin: 20px;
}

.container {
  max-width: 500px;
}

#add-form {
  margin-bottom: 20px;
}

#add-form input {
  margin-right: 5px;
  padding: 5px;
}

#add-form button {
  padding: 5px 10px;
}

#items-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

#items-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px;
  border: 1px solid #ccc;
  margin-bottom: 5px;
}

.delete-btn {
  margin-left: 10px;
  padding: 5px 10px;
  background: #f44336;
  color: #fff;
  border: none;
  cursor: pointer;
}







document.addEventListener('DOMContentLoaded', () => {
  const form = document.getElementById('add-form');
  const nameInput = document.getElementById('item-name');
  const descInput = document.getElementById('item-desc');
  const list = document.getElementById('items-list');

  // Добавление нового элемента
  form.addEventListener('submit', (e) => {
    e.preventDefault();

    const name = nameInput.value.trim();
    const desc = descInput.value.trim();

    if (!name) return;

    const li = document.createElement('li');

    li.innerHTML = `
      <div>
        <b>${name}</b><br>
        ${desc ? desc : ''}
      </div>
      <button class="delete-btn">Удалить</button>
    `;

    list.appendChild(li);

    nameInput.value = '';
    descInput.value = '';
    nameInput.focus();
  });

  // Удаление конкретного элемента
  list.addEventListener('click', (e) => {
    if (e.target.classList.contains('delete-btn')) {
      e.target.parentElement.remove();
    }
  });
});