Javascript DOM HTML Legend Object get

Introduction

The Legend object represents an HTML <legend> element.

We can access a <legend> element via document.getElementById():

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

Click the button to get the text of the legend element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<fieldset>
  <legend id="myLegend">MyNameFieldset:</legend>
  Name: <input type="text">
</fieldset>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {// w w  w .j a va  2 s .  co  m
  var x = document.getElementById("myLegend").innerHTML;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related