The read only form
property returns a reference to the form containing the label.
form |
Yes | Yes | Yes | Yes | Yes |
labelObject.form
A reference to the form element containing the label. If the label is not in a form, null is returned.
The following code shows how to get the id of the form containing the <label> element.
<!DOCTYPE html>
<html>
<body>
<!-- ww w . ja v a 2 s . co m-->
<form id="myForm">
<label id="myLabel" for="male">Male</label>
<input type="radio" name="sex" id="male" value="male"><br>
</form>
<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 code above is rendered as follows: