HTML | DOM Legend Object

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

Properties:

  • Form: It is used to return the reference of the form that contains the legend element.

Syntax:



document.getElementById("ID">);

Where “id” is the ID assigned to the “legend” tag.

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Legend Object</title>
    <style>
        form {
            width: 50%;
        }
          
        label {
            display: inline-block;
            float: left;
            clear: left;
            width: 90px;
            margin: 5px;
            text-align: left;
        }
          
        input[type="text"] {
            width: 250px;
            margin: 5px 0px;
        }
          
        .gfg {
            font-size: 40px;
            color: green;
            font-weight: bold;
        }
    </style>
</head>
  
<body>
    <div class="gfg">GeeksforGeeks</div>
    <h2>DOM Legend object</h2>
    <form>
        <fieldset>
            <!-- Assigning legend id -->
            <legend id="GFG">STUDENT::</legend>
            <label>Name:</label>
            <input type="text">
            <br>
            <label>Email:</label>
            <input type="text">
            <br>
            <label>Date of birth:</label>
            <input type="text">
            <br>
            <label>Address:</label>
            <input type="text">
            <br>
            <label>Enroll No:</label>
            <input type="text">
        </fieldset>
    </form>
    <br>
    <button onclick="myGeeks()">Submit</button>
    <script>
        function myGeeks() {
            
            // Accessing legend tag
            var g = document.getElementById("GFG");
            g.style.color = "coral";
            g.style.fontSize = "20px";
        }
    </script>
</body>
  
</html>         

chevron_right


Output:

Before Clicking On Button :

After Clicking On Button:

Example-2 : Legend Object can be created by using the document.createElement method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM Legend Object</title>
    <style>
        form {
            width: 50%;
        }
          
        label {
            display: inline-block;
            float: left;
            clear: left;
            width: 90px;
            margin: 5px;
            text-align: left;
        }
          
        input[type="text"] {
            width: 250px;
            margin: 5px 0px;
        }
          
        .gfg {
            font-size: 40px;
            color: green;
            font-weight: bold;
        }
    </style>
</head>
  
<body>
    <div class="gfg">GeeksforGeeks</div>
    <h2>DOM Legend object</h2>
    <form>
        <fieldset id="GFG">
  
            <label>Name:</label>
            <input type="text">
            <br>
            <label>Email:</label>
            <input type="text">
            <br>
            <label>Date of birth:</label>
            <input type="text">
            <br>
            <label>Address:</label>
            <input type="text">
            <br>
            <label>Enroll No:</label>
            <input type="text">
        </fieldset>
    </form>
    <br>
    <button onclick="myGeeks()">Submit</button>
    <script>
        function myGeeks() {
            // Legend object created 
            var g = document.createElement("LEGEND");
            var f = document.createTextNode("STUDENT:");
            g.appendChild(f);
            document.getElementById("GFG").appendChild(g);
  
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Clicking On Button:

After Clicking on Button:

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

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :


Be the First to upvote.


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