how to add new
  • to
      onclick with javascript
  • 后端 未结 3 1424
    猫巷女王i
    猫巷女王i 2020-11-29 19:56

    How do I add a list element to an existing ul using a function from an onclick? I need it to add to this type of list ...

    3条回答
    •  轻奢々
      轻奢々 (楼主)
      2020-11-29 20:05

      First you have to create a li(with id and value as you required) then add it to your ul.

      Javascript ::

      addAnother = function() {
          var ul = document.getElementById("list");
          var li = document.createElement("li");
          var children = ul.children.length + 1
          li.setAttribute("id", "element"+children)
          li.appendChild(document.createTextNode("Element "+children));
          ul.appendChild(li)
      }
      

      Check this example that add li element to ul.

    提交回复
    热议问题