Javascript DOM HTML Document createElement() Method create paragraph element

Introduction

Create a <p> element with some text, use innerText to set the text, and append it to the document:

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

View in separate window

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

<script>
function myFunction() {/*from  w w  w .j  a v  a2 s  . co m*/
  var para = document.createElement("P");
  para.innerText = "This is a paragraph.";
  document.body.appendChild(para);
}
</script>

</body>
</html>



PreviousNext

Related