diff --git a/21_api_todos/index.html b/21_api_todos/index.html new file mode 100644 index 0000000..f3e62d7 --- /dev/null +++ b/21_api_todos/index.html @@ -0,0 +1,19 @@ + + + + + + Document + + +

Práce s API serverem

+ + + +
+
+
+ + + + \ No newline at end of file diff --git a/21_api_todos/js/script.js b/21_api_todos/js/script.js new file mode 100644 index 0000000..f5402c8 --- /dev/null +++ b/21_api_todos/js/script.js @@ -0,0 +1,31 @@ +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 => `

${todo.id} + - ${todo.title} + - ${todo.completed ? "Hotovo":"Nedokončen"}

`).join(""); + } + else if (data) { + todoResult.innerHTML = `

${data.id} + - ${data.title} + - ${data.completed ? "Hotovo":"Nedokončen"}

`; + } else { + todoResult.innerHTML = "

Úkol nenalezen

" + } + }).catch(error => { + console.error("Chyba při načítaní dat:",error); + todoResult.innerHTML = "Chyba při načítaní dat" + }) + +}) \ No newline at end of file