Javascript DOM HTML Input Submit formMethod Property get

Introduction

Find out which HTTP method that is used to submit the form to the server:

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

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>
  <input type="submit" id="mySubmit" formmethod="post" value="Submit">
</form>//from   w  w  w .  ja  va2s.co  m
<button onclick="myFunction()">Display formmethod</button>

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

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

</body>
</html>

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

The formmethod attribute sets the HTTP method for sending form-data to the action URL.

The formmethod attribute overrides the method attribute of the <form> element.

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

The form-data can be sent as

  • URL variables (method="get") or
  • HTTP post transaction (method="post").
Value Description
get Default. Appends the form-data to the URL in name/value pairs: 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