guskansma
guskansma

Reputation: 51

How to make HTMLCollection to display as a string

I have created an unordered list in a div element. It all appears where it should but it shows incorrect values. The value shown is: "[object HTMLHeadingElement]" for what should have been car models. Any suggestions for how I can make the car models display instead? Thank you!

var name = document.getElementsByTagName("h4");
var myList = document.createElement("ul");
for (i = 0; i < name.length; i++) {
    var listItem = document.createElement("a");
    var div = document.getElementsByTagName("div");
var listValue = document.createTextNode(name[i]);

    listItem.appendChild(listValue);
    myList.appendChild(listItem);
    lastdiv.appendChild(myList);

}

Upvotes: 0

Views: 155

Answers (1)

Archie
Archie

Reputation: 911

You need to read the text content of the h4. For that use name[i].textContent, as name[i] is a DOM Node.

var listValue = document.createTextNode(name[i].textContent);

Upvotes: 1

Related Questions