-1
function addList(value) {
      const list = document.getElementsByClassName("list-div ")
      var div = document.createElement("div");
      div.classList.add("list-new");
       div.innerHTML = `<div class="h5-div" >
          <h5 class="list" >${value}</h5>
          </div>
          <div class="actions" >
          <span class="action-span" ><button 
                  class="action-btn">done</button></span>
          <span class="action-span" ><button 
                 class="action-btn">edit</button></span>
          <span class="action-span" ><button 
                   class="action-btn">delete</button></span>
          </div>`;

      list.appendChild(div);
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • `getElementsByClassName` would return an array. maybe list[0] will work – Krishna Prashatt Apr 15 '19 at 12:54
  • The `list` is an HTML Collection instance and it doesn't have an `appendChild` method. You can't add to the DOM via that interface, to my knowledge; I'm not sure how it could make sense. You'll need to determine where in the DOM you want your new `
    ` to go, and then use DOM node APIs to add it (like `appendChild`).
    – Pointy Apr 15 '19 at 12:58

3 Answers3

0

Preferibly when you are attaching elements to a list with a function, the best option is provide an id instead of a class. If you want to use the class, you have to use [0] since the "const list" is an array. You can't append a child to an array, you need to provide the exact element.

0

Use list[0] since getElementsByClassName returns a HTMLCollection (array-like object):

list[0].appendChild(div);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

getElementsByClassName returns a HTML Collection. You might need to use list[0] depending on what you wish to do.

list[0].appendChild(div);

Also, you can consider using document.getElementByID('uniqueId') if you wish to get one unique list in your dom. You need to create a list as:

<ul id="uniqueId">
</ul>
Abhas Tandon
  • 1,859
  • 16
  • 26