How to get the id of a form containing a dropdown list with JavaScript?

In this tutorial, we will learn how to get the id of a form containing a dropdown list using JavaScript.

The id property of an HTML element provides a unique identifier. The id property value must be unique within the HTML document. JavaScript uses it to access and manipulate elements with the specified id.

A dropdown menu (also called a select element) provides a list of options where users can choose one item from multiple available options.

Following are the methods to get the id of a form containing a dropdown list with JavaScript:

Using the form Property

In JavaScript, use the form property to get the id of a form containing a dropdown list. Every form control element has a form property that references its parent form element.

Syntax

var formId = document.getElementById("selectElementId").form.id;

The getElementById() method fetches the dropdown element, then .form.id gets the parent form's id.

Example

In this example, we use the form property to get the id of a form containing a dropdown list. The form has id "courseForm" and contains a select element with JavaScript course options.

<html>
<body>
   <p>Get the id of a form containing dropdown list using <i>form property</i> in JavaScript</p>
   
   <form id="courseForm">
      <p>Select JavaScript course level:</p>
      <select id="courseSelect">
         <option>Basic</option>
         <option>Intermediate</option>
         <option>Advanced</option>
         <option>Expert</option>
      </select>
   </form>
   
   <button onclick="getFormId()">Get Form ID</button>
   <p id="result"></p>
   
   <script>
      function getFormId() {
         var formId = document.getElementById("courseSelect").form.id;
         document.getElementById("result").innerHTML = "Form ID: " + formId;
      }
   </script>
</body>
</html>
Form ID: courseForm

Using jQuery closest() Method

jQuery provides the closest() method to traverse up the DOM tree and find the nearest ancestor element that matches a selector. We can use it to find the parent form of a dropdown element.

Syntax

var formId = $("#selectElementId").closest("form").attr("id");

The closest("form") finds the nearest form ancestor, then attr("id") gets its id attribute.

Example

This example uses jQuery to get the form id. We select the dropdown element and use closest() to find its parent form.

<html>
<head>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
   <p>Get form id using jQuery closest() method</p>
   
   <form id="skillForm">
      <p>Select your skill level:</p>
      <select id="skillSelect">
         <option>Beginner</option>
         <option>Intermediate</option>
         <option>Advanced</option>
         <option>Expert</option>
      </select>
   </form>
   
   <button onclick="getFormIdWithJQuery()">Get Form ID</button>
   <p id="jqueryResult"></p>
   
   <script>
      function getFormIdWithJQuery() {
         var formId = $("#skillSelect").closest("form").attr("id");
         document.getElementById("jqueryResult").innerHTML = "Form ID: " + formId;
      }
   </script>
</body>
</html>
Form ID: skillForm

Comparison

Method Dependency Browser Support Performance
form Property None (Vanilla JS) All modern browsers Fast
jQuery closest() jQuery library All browsers with jQuery Slightly slower

Key Points

  • The form property is the most direct way to access a form control's parent form
  • jQuery's closest() method is more flexible for complex DOM traversal
  • Both methods work reliably for getting form ids
  • Choose the vanilla JavaScript approach if you don't need jQuery for other functionality

Conclusion

Use the form property for simple form id retrieval with vanilla JavaScript, or jQuery's closest() method when working with jQuery-based applications. Both approaches effectively identify the parent form of dropdown elements.

Updated on: 2026-03-15T23:18:59+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements