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

JavascriptWeb DevelopmentFront End Scripts

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 −

Live Demo

<!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>
raja
Published on 26-Feb-2018 13:08:41
Advertisements