Javascript DOM HTML DList Object create

Introduction

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

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

Click the button to create a DL, DT and DD element: a description list, with terms and descriptions.

View in separate window

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

<script>
function myFunction() {//from  w  ww .j  a  v a 2s  . co  m
  var x = document.createElement("DL");
  x.setAttribute("id", "myDL");
  document.body.appendChild(x);

  var y = document.createElement("DT");
  var txt1 = document.createTextNode("CSS");
  y.appendChild(txt1);
  y.setAttribute("id", "myDT");
  document.getElementById("myDL").appendChild(y);

  var z = document.createElement("DD");
  var txt2 = document.createTextNode("to style");
  z.appendChild(txt2);
  document.getElementById("myDL").appendChild(z);
}
</script>

</body>
</html>



PreviousNext

Related