0

Imagine there is an array with photos, and we want to create a list that shows all of them:

photoOptions = ["Blue.jpg", "Red.jpg", "Green.jpg"]

Here's the code that I have currently:

photoOptions = ["Red.jpg", "Blue.jpg", "Green.jpg"]
var thumbnailList = document.getElementById('product-options');
var elem = document.createElement("img");
var listItem = document.createElement("li").appendChild(elem)
var clone;
photoOptions.forEach(function(item, index) {
  elem.src = 'http://placehold.it/100?text=' + index;
  clone = listItem.cloneNode();
  thumbnailList.appendChild(clone);
});
<ul id="product-options"></ul>

It creates the ul I want, but instead of the <img>'s living inside of an <li>, they are just listed as <img> elements with no parent <li>. What can I do to make the code return a <ul> with <li>'s that each contain a <img> tag?

2 Answers 2

1

The reason is that your var listItem is being set to the return value of appendChild, not to the li element. Also, you need to deep clone in order to copy your image element as well.

photoOptions = ["Red.jpg", "Blue.jpg", "Green.jpg"]
var thumbnailList = document.getElementById('product-options');
var elem = document.createElement("img");
var listItem = document.createElement("li");
listItem.appendChild(elem)
var clone;
photoOptions.forEach(function(item, index) {
  elem.src = 'http://placehold.it/100?text=' + index;
  clone = listItem.cloneNode(true);
  thumbnailList.appendChild(clone);
});
<ul id="product-options"></ul>

Also, it looks like placehold.it doesn't like text=0, not sure why.

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

Comments

1

The answer has to do with object references. Let me explain what the code is doing:

var listItem = document.createElement("li").appendChild(elem)

The right-hand expression here evaluates to the IMG element. Because the "equals" assignment operator assigns the value of the expression document.createElement("li").appendChild(elem) to listItem. The expression evaluates the createElement method, then the appendChild method, and "returns" that last method's return value.

According to MDN's Node.appendChild() spec, "The returned value is the appended child." So, after this line, listItem points to the IMG element.

  clone = listItem.cloneNode();
  thumbnailList.appendChild(clone);

Then, here, the IMG element is cloned and added to the UL.


Solution:

Make listItem reference the LI instead of the IMG:

var listItem = document.createElement("li");
listItem.appendChild(elem);

This code results in listItem pointing to the LI element, and also an IMG is appended to the LI without returning itself to listItem.

1 Comment

P.S. Debugging your code with breakpoints would have shown you this error super quickly.

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.