Javascript Reference - HTML DOM Input Submit formMethod Property








The formMethod property sets or gets the formmethod attribute of a submit button.

The formmethod attribute from input submit element overrides the method attribute of the <form> element and defines the HTTP method for sending form data to the action URL.

The form-data can be sent as URL variables with formMethod ="get" or as an HTTP post data with formMethod ="post".

Browser Support

formEnctype Yes 10.0 Yes Yes Yes




Syntax

Return the formMethod property:

submitObject.formMethod

Set the formMethod property:

submitObject.formMethod=get|post

Property Values

ValueDescription
getDefault. Appends the form-data to the URL in name/value pairs: URL?name=value&name=value
postSends the form-data as an HTTP post transaction

Return Value

A String type value representing the HTTP method used to submit the form to the server.





Example

The following code shows how to change the form method.

<!DOCTYPE html>
<html>
<body>
<form target="_blank">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" id="mySubmit" formaction="form.asp" formmethod="get" value="Submit">
</form>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
    document.getElementById("mySubmit").formMethod = "post";
    document.getElementById("mySubmit").formAction = "demo_form_method_post.asp";
}
</script>

</body>
</html>

Example 2

The following code shows how to get the form method value from input submit.


<!DOCTYPE html>
<html>
<body>
<!--   ww  w.  ja  v a  2 s. co  m-->
<form action="form.asp" method="get">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" id="mySubmit" formaction="demo.asp" formmethod="post" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("mySubmit").formMethod;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: