HTML DOM DFN object


The HTML DOM DFN object is associated with the HTML <dfn> element. The text inside the <dfn> element is the one being defined in the surrounding context. The DFN object represents the <dfn> element.

Syntax

Following is the syntax for −

Creating a DFN object −

var p = document.createElement("DFN");

Example

Let us look at an example for the HTML DOM DFN object −

Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>DFN object example</h2>
<p>Click the below button to create a dfn element with some text inside it.</p>
<button onclick="dfnCreate()">CREATE</button> <br><br>
<script>
   function dfnCreate() {
      var x = document.createElement("DFN");
      var t = document.createTextNode("The defination element");
      x.appendChild(t);
      document.body.appendChild(x);
   }
</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 dfnCreate() function when clicked by the user −

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

The dfnCreate() method creates a <dfn> element using the createElement() method of the document object. It then creates a text node using the createTextNode() method. The text node is then appended as a child to the <dfn> element. Finally the <dfn> element along with the text node are appended as the child of the document body using the appendChild() method −

function dfnCreate() {
   var x = document.createElement("DFN");
   var t = document.createTextNode("The definition element");
   x.appendChild(t);
   document.body.appendChild(x);
}

Updated on: 15-Feb-2021

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements