'how to retrieve JSON data as checkbox and to check those values which are true in javascript
Using AJAX I have retrieved data from API https://jsonplaceholder.typicode.com/todos (JSON data) as checkbox.For those values which are true the checkbox should be checked.
Solution 1:[1]
You can map and join:
fetch("https://jsonplaceholder.typicode.com/todos")
.then(response => response.json())
.then(data => document.getElementById("container").innerHTML = data
.map(item => `<div class="task">
<span class="userid">${item.userId}</span> <span class="id">${item.id}</span>
<h3>${item.title}</h3>
<label>Completed: <input type="checkbox" class="completed"${item.completed?" checked":""} /></label>
</div>`)
.join("")
);
.task { border: 1px solid black }
<div id="container"></div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mplungjan |
