Button formMethod Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Button

Description

The formMethod property sets or gets the formmethod attribute of a button, which sets which HTTP method to use when sending the form-data.

This attribute overrides the form's method attribute.

The formmethod attribute is only used for buttons with type="submit".

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

Set the formMethod property with the following Values

Value Description
get Appends 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 that is used to submit the form to the server

The following code shows how to return which HTTP method to use when sending the form-data:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="#" method="get">
First name: <input type="text" name="fname" value="Mary"><br>
Last name: <input type="text" name="lname" value="Bond"><br>
<button type="submit">Submit</button>
<button id="myBtn" type="submit" formmethod="post">Submit using POST</button>
</form>//from  ww  w  .  j  a  v a 2s .c om

<button onclick="myFunction()">Return formmethod of the second button</button>

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

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

</body>
</html>

Related Tutorials