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

HTMLJavascriptProgramming Scripts

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.

Live Demo

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