Javascript DOM HTML Input Button form Property get

Introduction

Return the id of the form containing the <input type="button"> element:

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

Click the input button to return the id of the form it belongs to.

View in separate window

<!DOCTYPE html>
<html>
<body>
<form id="myForm">
  <input type="button" onclick="myFunction()" id="myBtn" value="Test">
</form>// w w  w  . ja  v  a  2s .  c  o  m
<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 input button.

It returns a form object on success.

This property is read only.

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




PreviousNext

Related