HTML DOM Label Object
The DOM Label Object is used to represent the <label> element. The Label element is accessed by getElementById().
Properties:
Syntax:
document.getElementById("ID");
Where “id” is the ID assigned to the “label” tag.
Example 1: In this example, we will see the use of DOM Label Object.
HTML
<!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" >w3wiki</ 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 = "myBeginner()" >Submit</ button > < p id = "sudo" ></ p > < script > function myBeginner() { let g = document.getElementById("GFG").htmlFor; document.getElementById("sudo").innerHTML = g; } </ script > </ body > </ html > |
Output:
Example 2: Label Object can be created by using the document.createElement() method.
HTML
<!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" >w3wiki</ h1 > < h2 >DOM Label Object</ h2 > < form id = "GFG" > < input type = "radio" name = "Occupation" id = "Student" value = "student" > </ form > < button onclick = "myBeginner()" >Submit</ button > < script > function myBeginner() { let g = document.createElement("LABEL"); let f = document.createTextNode("Student"); g.setAttribute("for", "Student"); g.appendChild(f); document.getElementById("GFG").insertBefore( g, document.getElementById("Student")); } </ script > </ body > </ html > |
Output:
Supported Browsers: The browser supported by DOM Label Object are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari