HTML | DOM Label Object
The DOM Label Object is used to represent the <label> element. The Label element is accessed by getElementById().
Properties:
- control: It is used to return the labeled control.
- form: It is used to return the address of the form that contains the label.
- htmlFor: It is used to set or return the value of the for attribute of a label.
Syntax:
document.getElementById("ID");
Where “id” is the ID assigned to the “label” tag.
Example-1:
<!DOCTYPE html> <html> <head> <title>DOM Label Object</title> <style> body { font-size: 20px; } </style> </head> <body style="text-align:center"> <h1 style="color:green">GeeksforGeeks</h1> <h2>DOM Label Object</h2> <form> <!-- Starts label tag from here --> <label for="student" id="GFG"> Student </label> <input type="radio" name="Occupation" id="student" value="student"> <br> <label for="business"> Business </label> <input type="radio" name="Occupation" id="business" value="business"> <br> <label for="other"> Other </label> <!-- Ends label tags here --> <input type="radio" name="Occupation" id="other" value="other"> </form> <br> <button onclick="myGeeks()">Submit</button> <p id="sudo"></p> <script> function myGeeks() { var g = document.getElementById("GFG").htmlFor; document.getElementById("sudo").innerHTML = g; } </script> </body> </html> |
Output:
Before Clicking On Button:

After Clicking On Button:

Example-2: Label Object can be created by using the document.createElement method.
<!DOCTYPE html> <html> <head> <title>DOM Label Object</title> <style> body { font-size: 20px; } </style> </head> <body style="text-align:center;"> <h1 style="color:green">GeeksforGeeks</h1> <h2>DOM Label Object</h2> <form id="GFG"> <input type="radio" name="Occupation" id="Student" value="student"> </form> <button onclick="myGeeks()">Submit</button> <script> function myGeeks() { var g = document.createElement("LABEL"); var f = document.createTextNode("Student"); g.setAttribute("for", "Student"); g.appendChild(f); document.getElementById("GFG").insertBefore( g, document.getElementById("Student")); } </script> </body> </html> |
Output:
Before Clicking On Button:

After Clicking On Button:

Supported Browsers: The browser supported by DOM Label Object are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | label Tag
- HTML | label Attribute
- HTML <label> for Attribute
- HTML | <optgroup> label Attribute
- HTML | <track> label Attribute
- HTML | DOM Track label property
- HTML | DOM OptionGroup label Property
- HTML | <option> label Attribute
- HTML | DOM Option label Property
- HTML | DOM Label htmlFor Property
- HTML | <label> form Attribute
- How to create an HTML checkbox with a clickable label?
- HTML | DOM Object Object
- HTML | DOM Div Object
- HTML | DOM S Object
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.



