1

I have an array in javascript called menuElm that has <ul> elements in it:

<ul id="1"></ul>
<ul id="2"></ul>
<ul id="3"></ul>

I have a page in HTML that has the following:

<ul id="menu">
    <li class="menu-item"></li>
    <li class="menu-item"></li>
    <li class="menu-item"></li>
</ul>

I want to add the elements of menuElm to the HTML page so it would look like this:

<ul id="menu">
    <li class="menu-item">
         <ul id="1"></ul>
    </li>
    <li class="menu-item">
         <ul id="2"></ul>
    </li>
    <li class="menu-item">
         <ul id="3"></ul>
    </li>
</ul>

I have tried the following, but the <ul> elements just wont show up in the page nor in the code:

function CreateMenu() {
    var menuElm;
    var k = 0;

    menuElm = createElm("ul");
    menuElm.id = ++k;

    for (var i = 0; i < menuElm.length; ++i) {
        document.getElementsByClassName("menu-item")[i].appendChild(menuElm[i]);
    }
}

I am new with JavaScript, what am I doing wrong?

3
  • it is best avoid using number as ID. see here Commented Nov 25, 2016 at 14:29
  • You're declaring var k = 0 in the function so menuEl.id will always be 1. Also it's unclear what createElm("ul") is returning, assuming it's a shortcut for document.createElement(). But either way when CreateMenu run the loop is pretty useless as menuElm will always be a single element so not right to use menuElm.length in that case. You're better off saving the result of document.getElementsByClassName("menu-item") and loop over the length of that. Commented Nov 25, 2016 at 14:30
  • I changed menuElm.length to 3, and added the createElm("ul") into the loop, which is doing the same as document.creatElement("ul") Now it is creating the 3 ul-s with different id-s as it should be, although it doesnt add it into the classes Commented Nov 25, 2016 at 14:34

2 Answers 2

4
menuElm.length

The ul element doesn't have a length, so you are looping from 0 to 0, which is 0 iterations.


menuElm = createElm("ul");

This function isn't defined. You need document.createElement('ul');


menuElm = createElm("ul");
menuElm.id = ++k;

You appear to be creating one list item, and then changing its ID and appending it multiple times.

You need a new list item each time you go around the loop.


appendChild(menuElm[i]);

You've been treating menuElm as an element previously. It isn't an array, [i] makes no sense here.

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

2 Comments

Also, ++k is returning the value before incrementing, so you'd end up with 0 1 2 instead of 1 2 3 if this would have worked.
Nooooooooooo. NOOOOOOOO. not again with Quentin. Why do I keep failing while supporting ><.
0
$("#menu").find('li').each(function(i){
    $(this).append(menuElm[i]);
});

/* if you want to use jquery here is the code to append */

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.