Javascript DOM HTML Li Object create

Introduction

We can create a <li> element via the document.createElement() method:

var x = document.createElement("LI");

View in separate window

<!DOCTYPE html>
<html>
<body>

<ol id="myList">
  <li>Java</li>
  <li>CSS</li>
  <li>HTML</li>
</ol>/*from   ww w.  ja v a 2  s. c  o  m*/

<p>Click the button to create a LI element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.createElement("LI");
  var t = document.createTextNode("Coffee");
  x.appendChild(t);
  document.getElementById("myList").appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related