Javascript DOM HTML Pre Object create

Introduction

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

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

Click the button to create a PRE element.

View in separate window

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

<script>
function myFunction() {/*w  w w .jav a 2 s.  c  o  m*/
  var x = document.createElement("PRE");
  var t = document.createTextNode("This is a preformatted text.");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related