Javascript DOM HTML Paragraph Object create

Introduction

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

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

Click the button to create a P element.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

<script>
function myFunction() {/*from   w  ww .  j  av a 2s .co m*/
  var x = document.createElement("P");
  var t = document.createTextNode("This is a paragraph.");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related