Javascript DOM HTML Heading Object create

Introduction

We can create a heading element via the document.createElement() method:

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

Click the button to create a H1 element with some text.

View in separate window

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

<script>
function myFunction() {/* ww w  . j  a  v a2  s .c  o m*/
  var x = document.createElement("H1");
  var t = document.createTextNode("Welcome to My Homepage");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related