Witam,
robie małe zadanka, aby utrwalić sobie js'a i zrobilem taka liste wydatków, której wogóle nie stylowalem, więc nie zwracajcie uwagi na wygląd. Chciałem sie skupić tylko na JS i teraz wszystko śmiga,ale problem jest wtedy gdy inputy są puste,a i tak dodają mi sie <li> do <ul>, a chciałbym zeby dodawało mi tylko elementy do listy, gdy będą one wpisane w inputach. Wpisywałem juz wszystko w if, kombinowałem i dalej nie wiem co mozna tam wpisac ? chyba, ze poprostu kod jest gdzieś zle napisany lub umieszczony ?. Prosiłbym tez o ocenienie tego zadania, czy według Was wszystko jest ok, pomijając oczywiście problem z inputami i wygląd. Pozdrawiam i dziekuję za opowiedzi :)
<!DOCTYPE html>
<html lang="pl">
<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">
<title>Document</title>
<style>
ul {
background-color: blanchedalmond;
display: inline;
}
ul li {
background-color: pink;
padding: 0 20px;
display: inline;
}
</style>
</head>
<body>
<h1>Wydatki!</h1>
<label for="nazwa">Nazwa:</label>
<input id= "nazwa"type="text" placeholder="Miejsce wydatku">
<label for="date">Data:</label>
<input id="date" type="date">
<label for="kwota">Kwota:</label>
<input id="kwota" type="number">
<button id="dodaj">Dodaj wydatek</button>
<div>
<span>Nazwa:
<ul></ul>
</span>
<span>Data:
<ul></ul>
</span>
<span>Kwota:
<ul></ul>
</span>
<span>złotych
<ul></ul>
</span>
</div>
<script src="app.js"></script>
</body>
</html>
const nameInput = document.getElementById('nazwa');
const dateInput = document.getElementById('date');
const priceInput = document.getElementById('kwota');
const addBtn = document.getElementById('dodaj');
const expances = document.querySelectorAll('ul');
addBtn.addEventListener('click', () => {
const newExpances = document.createElement('li');
expances.forEach(expance => {
expance.appendChild(newExpances.cloneNode(true))
})
let firstChild = expances[0].lastChild;
let secondChild = expances[1].lastChild;
let thirdChild = expances[2].lastChild;
let forthChild = expances[3].lastChild;
firstChild.textContent = nameInput.value;
secondChild.textContent = dateInput.value;
thirdChild.textContent = priceInput.value;
// dodanie btn do usuwania <li></li>
const deleteBtn = document.createElement('button');
forthChild.appendChild(deleteBtn);
deleteBtn.textContent = "Usuń";
// // event na btn do usuwania <li></li>
deleteBtn.addEventListener('click', function removeExpances () {
forthChild.removeChild(deleteBtn);
expances[0].removeChild(expances[0].lastChild);
expances[1].removeChild(expances[1].lastChild);
expances[2].removeChild(expances[2].lastChild);
expances[3].removeChild(expances[3].lastChild);
})
// // if do sprawdzania inputow
if (nameInput.value === "") {
}
if (dateInput.value === "") {
}
if (priceInput.value === "") {
}
// if do czyszcznie inputow po dodaniu li do ul
if (firstChild.textContent === nameInput.value) {
nameInput.value = "";
}
if (secondChild.textContent === dateInput.value) {
dateInput.value = "";
}
if (thirdChild.textContent === priceInput.value) {
priceInput.value = "";
}
})