Javascript DOM HTML Button formMethod Property get

Introduction

Return which HTTP method to use when sending the form-data:

var x = document.getElementById("myBtn").formMethod;

Click "Return method" button to return the value of the formmethod attribute of the submit button.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php" method="get">
  First name: <input type="text" name="fname" value="CSS"><br>
  Last name: <input type="text" name="lname" value="HTML"><br>
  <button id="myBtn" type="submit" formmethod="post">Submit</button>
</form>/*from w  w  w .  ja v a  2 s.  c  o m*/

<button onclick="myFunction()">Return method</button>

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

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

</body>
</html>

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

The formmethod attribute sets 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")
  • HTTP post (with method="post").
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

The formMethod property returns a String representing the HTTP method that is used to submit the form to the server.




PreviousNext

Related