You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.1 KiB
31 lines
1.1 KiB
document.getElementById("fetchTodo").addEventListener("click", function(){
|
|
const todoId = document.getElementById("todoId").value;
|
|
const todoResult = document.getElementById("todoResult");
|
|
todoResult.innerHTML = "Načítání...";
|
|
|
|
let url = "https://jsonplaceholder.typicode.com/todos/";
|
|
if (todoId !== "" && todoId > 0){
|
|
url += todoId;
|
|
}
|
|
console.log(url);
|
|
|
|
fetch(url).then(response => response.json())
|
|
.then(data => {
|
|
if (Array.isArray(data)){
|
|
todoResult.innerHTML = data.map(todo => `<p><b>${todo.id}</b>
|
|
- ${todo.title}
|
|
- ${todo.completed ? "Hotovo":"Nedokončen"}</p>`).join("");
|
|
}
|
|
else if (data) {
|
|
todoResult.innerHTML = `<p><b>${data.id}</b>
|
|
- ${data.title}
|
|
- ${data.completed ? "Hotovo":"Nedokončen"}</p>`;
|
|
} else {
|
|
todoResult.innerHTML = "<p> Úkol nenalezen</p>"
|
|
}
|
|
}).catch(error => {
|
|
console.error("Chyba při načítaní dat:",error);
|
|
todoResult.innerHTML = "Chyba při načítaní dat"
|
|
})
|
|
|
|
})
|