Computer >> Computer tutorials >  >> Programming >> Javascript

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


To get the id of a form with a drop-down list, use the id property in JavaScript.

Example

You can try to run the following code to find the id of a form containing drop-down list −

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm1">
         <button id="btn" type="button" value="my_button">Demo</button>
      </form><br>
      <form id="myForm2">
         <select id="mySelect">
            <option>One</option>
            <option>Two</option>
            <option>Three</option>
         </select>
      </form>
      <script>
         var val = document.getElementById("mySelect").form.id;
         document.write("<br>Form id with dropdown: "+val);
      </script>
   </body>
</html>