Javascript DOM HTML UL Object create

Introduction

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

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

Click the button to create a UL and LI element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from w  ww .  j  a  v a2 s.c o m*/
  var x = document.createElement("UL");
  x.setAttribute("id", "myUL");
  document.body.appendChild(x);

  var y = document.createElement("LI");
  var t = document.createTextNode("Tea");
  y.appendChild(t);
  document.getElementById("myUL").appendChild(y);
}
</script>

</body>
</html>



PreviousNext

Related