0

I am trying to generate an unordered list and also navigation bar using JavaScript.

This is the JS snippet I have (looping through an Array using a forEach method):

const navMenu = document.querySelectorAll("page_header");
const navList = document.getElementById("menu_list");
const items = Array.from(document.querySelectorAll("section"));
items.forEach((item, i) => {

    const listItem = document.createElement("li");
    listItem.classList.add("menu-item");
    const anchorTag = document.createElement("a");
    anchorTag.innerText = item;
    const sectionName = item.getAttribute('data-nav');
    const sectionID = item.getAttribute('id');
    listItem.innerHTML = createNavItemHTML(sectionID, sectionName);
    navList.appendChild(listItem);

});

I also want to rewrite this function somehow since I took it straight off from a github repo that I am not comfy taking straight off.

 function createNavItemHTML(id, name) {
        const itemHTML = `<a class ="menu__link" data-id="${id}">${name}</a>`;
        return itemHTML;
    }

I simply want to include an anchor tag in my unordered list for each list item. But cannot get it to work...

Thanks in advance.

5
  • anchorTag.innerText = item; are you trying to put section elements into a elements? You cannot assign objects to innerText. Please add information as of what functionality you are trying to implement. Commented Nov 10, 2021 at 17:55
  • Oh, good question. This is a mistake I guess. I want to create my list items as "clickable" links. Directing them to relevant section on the page. Commented Nov 10, 2021 at 17:58
  • Those sections all have id and data-nav? Commented Nov 10, 2021 at 17:59
  • Yes, they all have. Commented Nov 10, 2021 at 18:25
  • Thanks @connexo for your input and clarification on innerText. The main functionality is to create an unordered list and present it in a navbar with anchor links to each section. The id's and data-nav attributes are retrieved from each of the 6 sections. I tried your snippet below and it works. Will just have to adjust so they are presented as a horizontal navBar. Thanks alot! Commented Nov 10, 2021 at 18:32

1 Answer 1

1

You can simplify your code alot:

const navList = document.getElementById("menu_list");
const sections = document.querySelectorAll("section");
for (const section of sections) {
    const li = document.createElement("li");
    li.className = "menu-item";
    const a = document.createElement("a");
    a.textContent = section.dataset.nav;
    a.href = `#${section.id}`;
    li.appendChild(a);
    navList.appendChild(li);
};

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks a lot for a swift and crisp response. @connexo Very much appreciated. I am new to this so excuse me for asking, but is it ok to use this snippet in my project, accrediting you of course?
That is trivial code, no need for attribution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.