<!DOCTYPE html>
<html>
<body>

<h3>A demonstration of how to access a LEGEND element</h3>

<fieldset>
  <legend id="myLegend">Personalia:</legend>
  Name: <input type="text">
</fieldset>

<p>Click the button to get the text of the legend element.</p>

<button onclick="myFunction()">Try it</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myLegend").innerHTML;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>