I have this code right now, but its not working on the DOM, I am wondering what is the problem with my code. My current code:
“JavaScript”
JavaScript
x
14
14
1
const sectionElements = document.querySelectorAll('section');
2
const menuList = document.getElementById('navbar__list');
3
4
function NavBarCreate (){
5
for (let i = 0; i < sectionElements.length; i++){
6
const listStart = document.createElement('li');
7
const attributeId = sectionElements[i].getAttribute('id');
8
const attributeName = sectionElements[i].getAttribute('data-nav');
9
listStart.innerHTML =
10
`<a href="${attributeId}" class="menu__link">${attributeName}</a>`;
11
menuList.appendChild(listStart);
12
}
13
}
14
“HTML”
JavaScript
1
11
11
1
<nav class="navbar__menu">
2
<ul id="navbar__list"></ul>
3
</nav>
4
<main>
5
<section id="section1" data-nav="Section 1">
6
</section>
7
8
<section id="section2" data-nav="Section 2">
9
</section>
10
</main>
11
<
Advertisement
Answer
call the function or avoid the function. try it