Javascript DOM HTML Form method Property set

Introduction

Change the method for sending form data:

document.getElementById("myForm").method = "post";

Click the "Change method" button to change the method in the form above.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="/action_page.php" method="get" target="_blank">
  First name: <input type="text" name="fname" value="CSS"><br>
  Last name: <input type="text" name="lname" value="HTML"><br>
  <input type="submit" value="Submit">
</form>//  ww w. j  a  v  a  2 s .c om
<button onclick="myFunction()">Change method</button>

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

<script>
function myFunction() {
  document.getElementById("myForm").method = "post";
  document.getElementById("demo").innerHTML = "The method was set to 'post'.";
}
</script>

</body>
</html>

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

The method attribute sets how to send form-data.

Property Values

Value Description
get Default. 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 used to submit the form (either "get" or "post")



PreviousNext

Related