Javascript DOM HTML Option form Property get

Introduction

Return the id of the form containing the <option> element with id="apple":

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

Click the button to return the id of the form the apple option belongs to.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm">
Select your favorite:
<select>
  <option id="apple">Apple</option>
  <option id="orange">Orange</option>
  <option id="pineapple">Pineapple</option>
  <option id="banana">Banana</option>
</select>//  w  ww.j a  va2s.c  o  m
</form>
<button type="button" onclick="myFunction()">Test</button>

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

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

</body>
</html>

The form property returns a reference to the form that contains an option.

This property returns a form object on success.

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




PreviousNext

Related