Javascript Reference - HTML DOM Legend Object








The Legend object represents an HTML <legend> element.

Legend Object Properties

Property Description
form Returns a reference to the form that contains the legend

Standard Properties and Events

The Legend object supports the standard properties and events.

Example

We can access a <legend> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<fieldset>
  <legend id="myLegend">Information:</legend>
  Name: <input type="text">
</fieldset>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  ww  w .j  ava 2 s.  co m-->
    var x = document.getElementById("myLegend").innerHTML;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <legend> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<fieldset id="myFieldset">
  Name: <input type="text">
</fieldset><br>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w w  w.j av a  2  s.  c  om-->
    var x = document.createElement("LEGEND");
    var t = document.createTextNode("Information:");
    x.appendChild(t);
    document.getElementById("myFieldset").appendChild(x);
}
</script>
</body>
</html>

The code above is rendered as follows: