Javascript DOM HTML Header Object create

Introduction

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

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

Click the button to create a HEADER element containing a h3 element with some text.

View in separate window

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

<script>
function myFunction() {// w  ww  .  j av  a 2  s .com
  var x = document.createElement("HEADER");
  x.setAttribute("id", "myHeader");
  document.body.appendChild(x);

  var y = document.createElement("H3");
  var t = document.createTextNode("This is a h3 element in a header element");
  y.appendChild(t);

  document.getElementById("myHeader").appendChild(y);
}
</script>

</body>
</html>



PreviousNext

Related