HTML | DOM HTML Object

The HTML Object property in HTML DOM is used to represent or access the HTML <html> element with in the object. The <html> element is used to return the HTML document as an Element Object.

Syntax:

  • It is used to access a <html> element.
    var x = document.getElementsByTagName("HTML")[0];
  • It can also be used to access a <html> element.
    var x = document.documentElement;

Property Values:



  • getElementsByTagName(): It is used to return a collection of all child elements with the specified tag name.
  • innerHTML: It is used to set or return the content of an element.
  • getElementsById(): It is used to return a collection of all child elements with the specified Id.

Example-1: Access HTML element using document.getElementsByTagName(“HTML”)[0];

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<title>
    HTML | DOM HTML Object Property
</title>
<style>
    body {
        text-align: center;
        width: 70%;
    }
      
    h1 {
        color: green;
    }
      
    h1,
    h2 {
        text-align: center;
    }
</style>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2> HTML Object</h2>
  
    <p>Click the button to get the 
      HTML content of the html element.</p>
  
    <button onclick="GFG()">Click</button>
  
    <p id="Geeks"></p>
  
    <script>
        function GFG() {
              
          // Access html element and 
          return using "innerHTML"
          var x = 
          document.getElementsByTagName(
            "HTML")[0].innerHTML;
          document.getElementById("Geeks").innerHTML = x;
      }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Click On the Button:

After Click On the Button:

Example-2: Access html element and return element is first or second.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<title>
    HTML | DOM HTML Object Property
</title>
<style>
    body {
        text-align: center;
        width: 70%;
    }
      
    h1 {
        color: green;
    }
      
    h1,
    h2 {
        text-align: center;
    }
</style>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2> HTML Object</h2>
  
    <p>Click the button to get the 
      HTML content of the html element.</p>
    <p>Using the document.documentElement</p>
    <button onclick="GFG()">Click</button>
  
    <p id="Geeks"></p>
  
    <script>
        function GFG() {
            // Access html element and return html 
           // with position value of html element.
            var x = 
            document.documentElement.innerHTML;
            document.getElementById(
              "Geeks").innerHTML = "first" + x;
            
            var y = 
            document.documentElement.innerHTML;
            document.getElementById(
              "Geeks").innerHTML = y + "second";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Click On the Button:

After Click On the Button:

Example-3: Access html element and return all child with specified tag name.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<title>
    HTML | DOM HTML Object Property
</title>
<style>
    body {
        text-align: center;
        width: 70%;
    }
      
    h1 {
        color: green;
    }
      
    h1,
    h2 {
        text-align: center;
    }
</style>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2> HTML Object</h2>
  
    <p>Click the button to get the
      HTML content of the html element.</p>
    <p>Using the getElementsByTagName("HTML")[0] 
      and documentElement</p>
    <button onclick="GFG()">Click</button>
  
    <p id="Geeks"></p>
  
    <script>
        function GFG() {
            // access and return html element
            var x = 
              document.getElementsByTagName(
                "HTML")[0].innerHTML;
            document.getElementById("Geeks").innerHTML =
              "getElementsByTagName" + x;
            var y = 
             document.documentElement.innerHTML;
            document.getElementById("Geeks").innerHTML =
              y + "documentElement";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Click On the Button:

After Click On the Button:

Supported Browsers: The browser supported by DOM HTML Object property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.