HTML DOM Article Object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Article object represents the HTML <article> element that was introduced in the HTML5. An article is a self-contained area in an HTML document. It is a part of the semantic tags introduced in HTML5.

Syntax

Following is the syntax for −

Creating an article object

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

Example

Let us see an example of HTML DOM article object −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h3>ARTICLE HEADING</h3>
<article id="ArticleObj">
<h1>Heading</h1>
<p>Sample Article Text</p>
</article>
<p>Click the below button to change article size and color</p>
<button onclick="ChangeArticle()">CHANGE</button>
<button onclick="AddArticle()">ADD</button>
<script>
   function ChangeArticle() {
      var x = document.getElementById("ArticleObj");
      x.style.color = "green";
      x.style.fontSize = "25px";
   }
   function AddArticle() {
      var x = document.createElement("ARTICLE");
      x.setAttribute("id", "myArticle");
      document.body.appendChild(x);
      var heading = document.createElement("H1");
      var txt1 = document.createTextNode("Append Article");
      heading.appendChild(txt1);
      document.getElementById("myArticle").appendChild(heading);
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click “CHANGE” button−

Click “ADD” button −

In the above example −

We first created an article with id “ArticleObj” and a header and a paragraph in it −

<article id="ArticleObj">
<h1>Heading</h1>
<p>Sample Article Text</p>
</article>

We then created two buttons named CHANGE and ADD to execute ChangeArticle() and AddArticle() function respectively

<button onclick="ChangeArticle()">CHANGE</button>
<button onclick="AddArticle()">ADD</button>

The function ChangeArticle() gets the element with id “ArticleObj” associated with it and changes its color and font size −

function ChangeArticle() {
   var x = document.getElementById("ArticleObj");
   x.style.color = "green";
   x.style.fontSize = "25px";
}

The function AddArticle() creates an element of type article first. It then using the setAttribute method assigns it “myArticle” id. The article element is then appended to the document body. Heading is created and some text is appended to it using the append child property. The heading along with the text content is then appended to the article with id “myArticle” −

function AddArticle() {
   var x = document.createElement("ARTICLE");
   x.setAttribute("id", "myArticle");
   document.body.appendChild(x);
   var heading = document.createElement("H1");
   var txt1 = document.createTextNode("Append Article");
   heading.appendChild(txt1);
   document.getElementById("myArticle").appendChild(heading);
}
raja
Published on 06-Aug-2019 13:00:11
Advertisements