Open In App

HTML DOM HR Object

The DOM HR Object is used to represent the HTML <hr> element. The hr element is accessed by getElementById()

Properties:



Syntax:

document.getElementById("ID");

Where “ID” represents the element id. 



Example 1: In this example, we will see the use of DOM HR Object.




<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM HR Object</title>
</head>
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM HR Object</h2>
    <p>There is a horizontal
        rule below this paragraph.</p>
    <!-- Assigning id to 'hr' tag. -->
    <hr id="GFG">
    <p>This is a horizontal rule above this paragraph.</p>
    <button onclick="myGeeks()">Try it</button>
    <script>
        function myGeeks() {
            // Accessing 'hr' tag.
            let x = document.getElementById("GFG");
            x.style.display = "none";
        }
    </script>
</body>
</html>

Output: 

 

Example 2: HR Object can be created by using the document.createElement Method. 




<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM HR Object
    </title>
</head>
<body>
    <h1 style="color:green;">GeeksForGeeks</h1>
    <h2>DOM HR Object</h2>
    <button onclick="myGeeks()">Submit</button>
    <script>
        function myGeeks() {
            let g = document.createElement("HR");
            document.body.appendChild(g);
        }
    </script>
</body>
</html>

Output: 

 

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


Article Tags :