Javascript Reference - HTML DOM Form method Property








The method property sets or gets the method attribute in a form.

The method attribute sets how to send form-data.

Browser Support

method Yes Yes Yes Yes Yes

Syntax

Return the method property.

var v = formObject.method

Set the method property.

formObject.method=get|post

Property Values

Value Description
get Appends the form-data to the URL. This is default.
post Sends the form-data as an HTTP post transaction




Return Value

A String type value representing the HTTP method used to submit the form, either 'get' or 'post'.

Example

The following code shows how to get the method for sending form data.


<!DOCTYPE html>
<html>
<body>
<!--from  ww w.  ja  v a2 s .  c  o  m-->
<form id="myForm" action="url" method="get">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the method for sending form data.


<!DOCTYPE html>
<html>
<body>
<!--from  ww w . ja v a2  s.  co m-->
<form id="myForm" action="demo.asp" method="get" target="_blank">
  First name: <input type="text" name="fname" value="abc"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myForm").method = "post";
    document.getElementById("myForm").action = "demo_form_method_post.asp";
}
</script>
</body>
</html>

The code above is rendered as follows: