Javascript Reference - HTML DOM Button formMethod Property








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

Browser Support

form Yes 10 Yes Yes Yes

Syntax

Return the formMethod property:

var v = buttonObject.formMethod 

Set the formMethod property:

buttonObject.formMethod=get|post

Property Values

Value Description
get Append the form-data to the URL: url?name=value&name=value
post Sends the form-data as an HTTP post transaction




Return Value

A String representing the HTTP method.

Example

The following code shows how to get which HTTP method.


<!DOCTYPE html>
<html>
<body>
<!--from w w  w .  ja  va2s  .  c  o  m-->
<form action="url" method="get">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" type="submit" formmethod="post" formaction="a new url">Submit</button>
</form>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myBtn").formMethod;
    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.


<!DOCTYPE html>
<html>
<body>
<!-- w  w w . j a v a2  s  .c o  m-->
<form target="_blank">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" formaction="a url" formmethod="get"  type="submit">Submit</button>
</form>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    document.getElementById("myBtn").formMethod = "post";
    document.getElementById("myBtn").formAction = "a new url";
    document.getElementById("demo").innerHTML = "changed.";
}
</script>

</body>
</html>

The code above is rendered as follows: