Javascript DOM HTML Legend form Property get

Introduction

Return the id of the form containing the <legend> element:

var x = document.getElementById("myLegend").form.id;

Click button to return the id of the form the legend belongs to.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm">
  <fieldset>
  <legend id="myLegend">MyNameFieldset:</legend>
  Name: <input type="text"><br>
  Email: <input type="text">
  </fieldset>
</form>/*from  w w  w  .j  a va 2s .c o m*/
<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 form property returns a reference to the form that contains the legend.

This property returns a form object on success.

This property is read only.

If the legend is not in a form, null is returned




PreviousNext

Related