Javascript DOM HTML Article Object create

Introduction

The Article object represents an HTML <article> element.

We can create an <article> element by using the document.createElement() method:

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

Click the button to create an ARTICLE element containing a H1 and P element.

View in separate window

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

<script>
function myFunction() {/*from  w w w . j a  v  a  2s  .c o m*/
  var x = document.createElement("ARTICLE");
  x.setAttribute("id", "myArticle");
  document.body.appendChild(x);

  var heading = document.createElement("H1");
  var txt1 = document.createTextNode("Heading");
  heading.appendChild(txt1);
  document.getElementById("myArticle").appendChild(heading);

  var para = document.createElement("P");
  var txt2 = document.createTextNode("Paragraph.");
  para.appendChild(txt2);
  document.getElementById("myArticle").appendChild(para);
}
</script>

</body>
</html>



PreviousNext

Related