Javascript DOM HTML Select form Property get

Introduction

Return the id of the form containing the drop-down list:

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

Click the button to return the id of the form the dropdown list belongs to.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm">
  Select your favorite:
  <select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
  </select>
</form>/*from w ww . j av  a  2s  . c o m*/

<button type="button" onclick="myFunction()">Test</button>

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

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

</body>
</html>

The form property returns a reference to the form that contains the drop-down list.

This property returns a form object on success.

If the drop-down list is not in a form, null is returned




PreviousNext

Related