Return the id of the form containing the <label> element:
var x = document.getElementById("myLabel").form.id;
Click the "Test" button to return the id of the form the label belongs to.
<!DOCTYPE html> <html> <body> <form id="myForm"> <label id="myLabel" for="male">Male</label> <input type="radio" name="sex" id="male" value="male"><br> </form>//from w w w .jav a 2s. c o m <p id="demo"></p> <button onclick="myFunction()">Test</button> <script> function myFunction() { var x = document.getElementById("myLabel").form.id; document.getElementById("demo").innerHTML=x; } </script> </body> </html>
The form property returns a reference to the form containing the label.
This property returns a form object on success.
This property is read only.
If the label is not in a form, null is returned