1

I have an unordered list that works, and it is as follow. but i need it to result in links instead of regular text.

working list:

    var navLinks = [
        "Eggs",
        "Milk",
        "Juice",
        "Diapers",
        "Bread"
    ];

    var ul = document.createElement("ul");

    for (var i = 0; i < navLinks.length; i++) {
        ul.innerHTML = ul.innerHTML + "<li>" + navLinks[i] + "</li>";
    }

    document.body.appendChild(ul);

thank you.

0

3 Answers 3

1

Replace:

ul.innerHTML = ul.innerHTML + "<li>" + navLinks[i] + "</li>";

With:

ul.innerHTML = ul.innerHTML + "<li><a href='#'>" + navLinks[i] + "</a></li>";

You need to decide what value you would like to have for the href property of these links.

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

2 Comments

You both have it right. but i can only check answer once, but you both have it right thank you. both
ahhhh, i need the hash to be navLinks[i] plus(+) .htm
0

You mean something like

 var navLinks = [
        "Eggs",
        "Milk",
        "Juice",
        "Diapers",
        "Bread"
    ];

    var ul = document.createElement("ul");

    for (var i = 0; i < navLinks.length; i++) {
        ul.innerHTML = ul.innerHTML + "<li><a>" + navLinks[i] + "</a></li>";
    }

    document.body.appendChild(ul)

Of course you should add an href property to the link so that it leads somewhere

2 Comments

You both have it right. but i can only check answer once, but you both have it right thank you. both
ahhhh, i need the hash to be navLinks[i] plus(+) .htm
0

Thank you Sarfraz and Nicola Peluchetti. final code:

var navLinks = [
    "Eggs",
    "Milk",
    "Juice",
    "Diapers",
    "Bread"
];

var ul = document.createElement("ul");
var e = ".htm";
for (var i = 0; i < navLinks.length; i++) {
    ul.innerHTML += "<li><a href='" + navLinks[i] + "" + e + "'>" + navLinks[i] + "</a></li>";
}

document.body.appendChild(ul)

Comments

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.