<!DOCTYPE html>
<html>
<body>

<form id="myForm">
  <fieldset>
    <legend id="myLegend">Personalia:</legend>
    Name: <input type="text"><br>
    Email: <input type="text">
  </fieldset>
</form>

<p>Click the "Try it" button to return the id of the form the legend belongs to.</p>

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

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

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

</body>
</html>