Open In App

HTML | DOM form method Property

Improve
Improve
Like Article
Like
Save
Share
Report

The DOM Form method Property is used to set or returnthe value of the method attribute in the form. The method attribute is used to specify the HTTP method used to send data while submitting the form. There are two kinds of HTTP Methods, which are GET and POST.

Syntax:

  • It is used to return the method property:
    formObject.method
  • It is used to Set the method property.
    formObject.method = get|post
  • Property Values

    • GET: It is the default value. In the GET method, after the submission of the form, the form values will be visible in the address bar of the new browser tab.
    • POST: In the post method, after the submission of the form, the form values will not be visible in the address bar of the new browser tab as it was visible in the GET method.

    Return Value It returns a string value which represent the HTTP method used to send data while submitting the form.

    Example-1: HTML Program that illustrate how to set the Property.




    <!DOCTYPE html>
    <html>
      
    <body style="text-align:center;">
        <h1 style="color:green;">
          GeeksForGeeks
      </h1>
        
        <h2>DOM Form method Property.</h2>
        <form id="users" 
              action="#" 
              method="GET"
              target="_blank">
            First name:
            
            <input type="text" 
                   name="fname"
                   value="Manas">
            
            <br> Last name:
            <input type="text"
                   name="lname" 
                   value="Chhabra">
            
            <br>
            <input type="submit" 
                   value="Submit">
        </form>
      
        <p>Click the "Try it" button to return the
          value contained in the form.</p>
      
        <button onclick="myGeeks()">Try it</button>
      
        <p id="sudo"
           style="font-size:25px;color:green;">
      </p>
      
        <script>
            function myGeeks() {
                
                // Set property of method.
                var x =
                    document.getElementById("users").method = 
                    "POST";
                document.getElementById("sudo").innerHTML = 
                  "The value of the method attribute was changet to "
                + x;
            }
        </script>
      
    </body>
      
    </html>

    
    

    Output :

    Before Clicking On Button:

    After Clicking On Button:

    Example-2: HTML Program illustrate how to return the property.




    <!DOCTYPE html>
    <html>
      
    <body style="text-align:center;">
        <h1 style="color:green;">
          GeeksForGeeks
      </h1>
        <h2>DOM Form method Property.
      </h2>
        
        <form id="users" 
              action="#" 
              method="GET" 
              target="_blank">
            First name:
            
            <input type="text" 
                   name="fname" 
                   value="Manas">
            <br> Last name:
            
            <input type="text"
                   name="lname"
                   value="Chhabra">
            <br>
            
            <input type="submit"
                   value="Submit">
        </form>
      
        <p>Click the "Try it" button to return 
          the value of the method attribute.</p>
      
        <button onclick="myGeeks()">
          Try it
      </button>
      
        <p id="sudo" 
           style="font-size:25px;color:green;">
      </p>
      
        <script>
            function myGeeks() {
                
                // Return the property value 
                var x = document.getElementById(
                  "users").method;
                document.getElementById(
                  "sudo").innerHTML = x;
            }
        </script>
      
    </body>
      
    </html>

    
    

    Output:

    Before Clicking On Button:

    After Clicking On Button:

    Supported Browsers: The browser supported by DOM Form method Property are listed below:

    • Google Chrome 1 and above
    • Edge 12 and above
    • Firefox 1 and above
    • Opera 12.1 and above
    • Safari 3 and above


    Last Updated : 31 Aug, 2022
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads