1 | initial version |
To create a dynamic checkbox using API data, you can follow these steps:
fetch()
or axios
.<div>
or <ul>
element.Here is an example code snippet that demonstrates how to create dynamic checkboxes using API data:
const apiUrl = 'https://example-api.com/data';
async function createCheckboxes() {
const response = await fetch(apiUrl);
const data = await response.json();
const container = document.createElement('div');
data.forEach(item => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = item.id;
checkbox.name = item.name;
checkbox.value = item.value;
const label = document.createElement('label');
label.setAttribute('for', item.id);
label.textContent = item.name;
container.appendChild(checkbox);
container.appendChild(label);
});
document.body.appendChild(container);
}
createCheckboxes();