First time question asker, go easy on me. I think I've thoroughly looked through Stack so as to not ask a repeated question.
I have a function.
function createDivs(num) {
for(let inc = 0; inc < num; inc++) {
let div = document.createElement('div'),
tab_content = document.getElementsByClassName('tab-content');
div.className = 'classname';
div.id = `number-${inc}`;
div.innerHTML = 'did it work?';
tab_content.innerHTML = div;
}
}
Where the HTML is just simply
<div class="tab-content">
<script>
createDivs(4);
</script>
</div>
I've tried other methods like
let tab_content = document.getElementsByClassName('tab-content');
tab_content.innerHTML = '<div class="classname" id=`number-${inc}`>did it work?</div>';
as well as ton other variations with the '' "" and `` and that didn't work neither.
I have also tried tab_content.appendChild(div); and that didn't work either.
In reality this is a downgrade of what my actual code is because I'm creating tons of elements that need things in them, but I can't even get this bit to work. It doesn't even work if I remove the for loop. Also I did see this post, hence why I tried the appendChild, but again that also didn't work.
getElementsByClassNamereturns a nodeList, which does not have an innerHTML property. You need to select a single node from that list, or iterate over each node and then set the innerHTML