const btnList = [
{
id: 'all',
content: 'All'
},
{
id: 'br',
content: 'Breakfast'
},
{
id: 'dn',
content: 'Dinner'
}
];
window.addEventListener('load', () => {
const btnz = document.getElementById('btnz');
let newBtn = '';
btnList.forEach(btn => {
newBtn += `<button id="${btn.id}" class="btn-item">${btn.content}</button>`
})
btnz.innerHTML = newBtn;
});
const btnCollection = document.getElementsByClassName("btn-item");
console.log(btnCollection.length);
And this is HTML:
<div id="btnz"></div>
I want to create dynamic buttons, then go over newly created buttons with forEach() function. It gave me an error, so I tried log btnCollection.length and for some reason it returns me '0' instead of '3'. Why?