Javascript Reference - HTML DOM Legend form Property








The form property is a is read only property that returns a reference to the form that contains the legend.

Browser Support

form Yes Yes Yes Yes Yes

Syntax

var v = legendObject.form 

Return Value

A reference to the form element containing the legend. If the legend is not in a form, null is returned.





Example

The following code shows how to ge the id of the form containing the <legend> element.


<!DOCTYPE html>
<html>
<body>
<!-- w ww . ja v  a 2  s  .  c  o  m-->
<form id="myForm">
  <fieldset>
    <legend id="myLegend">Information:</legend>
    Name: <input type="text"><br>
    Email: <input type="text">
  </fieldset>
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myLegend").form.id;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: