Javascript Reference - HTML DOM Option form Property








The form property gets the form that contains an option.

Browser Support

form Yes Yes Yes Yes Yes

Syntax

var v = optionObject.form 

Return Value

A reference to the form element containing the option. If the option is not in a form, null is returned.





Example

The following code shows how to get the id of the form containing the <option> element with id="C".


<!DOCTYPE html>
<html>
<body>
<!--  w w w . jav  a 2s  . co  m-->
<form id="myForm">
Select your favorite fruit:
<select>
  <option id="C">C</option>
  <option id="A">A</option>
  <option id="P">P</option>
  <option id="B">B</option>
</select>
</form>
<button type="button" onclick="myFunction()">Try it</button>

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

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

</body>
</html>

The code above is rendered as follows: