Form method Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Description

The method property sets or gets the method attribute in a form, which controls how to send form-data.

Set the method property with the following 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")

The following code shows how to change the method for sending form data:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="#" method="get" target="_blank">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br>
  <input type="submit" value="Submit">
</form>//from ww w.ja  va2 s  .  c o  m

<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>

Related Tutorials