HTML DOM emphasized object


The HTML DOM emphasized object is associated with the HTML <em> element. The <em> element is used for emphasizing some text and it marks that text in italic. You can create and access emphasized object using createElement() or getElementById() method respectively.

Syntax

Following is the syntax for −

Creating an emphasized object −

var e = document.createElement("EM");

Example

Let us look at an example for the emphasized object −

<!DOCTYPE html>
<html>
<body>
<h1>emphasized object example</h1>
<p>Create an em element by clicking the button below</p>
<button onclick="createEM()">CREATE</button>
<br><br>
<script>
   function createEM() {
      var e = document.createElement("EM");
      var txt = document.createTextNode("HELLO WORLD");
      e.appendChild(txt);
      document.body.appendChild(e);
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CREATE button −

We have first created a button CREATE that will execute the createEM() method when clicked by the user.

<button onclick="createEM()">CREATE</button>

The createEM() method creates an <em> element using the createElement() method of the document object. It then assigns the element to variable e and creates a text node with some text using the createTextNode() method of the document object. The text node is then appended to the <em> element. Finally the <em> element along with the text node are appended to the document’s body using the appendChild() method −

function createEM() {
   var e = document.createElement("EM");
   var txt = document.createTextNode("HELLO WORLD");
   e.appendChild(txt);
   document.body.appendChild(e);
}
raja
Published on 16-Aug-2019 10:26:25
Advertisements