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

How to find the id of the form a button belongs to in JavaScript?


To get the id of the form in JavaScript, use the id property.

Example

You can try to run the following code to learn how to get the id of the form in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm">
         <button id="btn" type="button">My Button</button>
      </form>
      <script>
         var str = document.getElementById("btn").form.id;
         document.write("Form id of above button: "+str);
      </script>
   </body>
</html>