Javascript DOM HTML Button form Property

Introduction

Return the id of the form the button belongs to:

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>
<form id="myForm">
  <button id="myBtn" type="button" onclick="myFunction()">Test</button>
</form>/* ww w . ja v a2 s. com*/

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

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

</body>
</html>

The form property returns a reference to the form containing the button.

The form property returns a form object on success.

This property is read only.

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




PreviousNext

Related