Javascript Reference - HTML DOM Form action Property








The action property sets or gets the action attribute in a form.

The action attribute sets where to send the form data when a form is submitted.

Browser Support

action Yes Yes Yes Yes Yes

Syntax

Return the action property.

var v = formObject.action

Set the action property.

formObject.action=URL




Property Values

Value Description
URL Set where to send the form data when the form is submitted.

Return Value

A String type value representing the URL of where to send the form-data when a form is submitted.

Example

The following code shows how to get the URL for where to send the form data when a form is submitted.


<!DOCTYPE html>
<html>
<body>
<!-- w w w . j  a  v  a  2  s . c  om-->
<form id="myForm" action="url">
  First name: <input type="text" name="fname" value="abc"><br>
  Last name: <input type="text" name="lname" value="def"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myForm").action;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the action URL of a form.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w . j a v a2  s.c  o m-->
<form id="myForm" action="nothing.asp">
  First name: <input type="text" name="fname" value="abc"><br>
  Last name: <input type="text" name="lname" value="def"><br>
  <input type="submit" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myForm").action = "url";
}
</script>

</body>
</html>

The code above is rendered as follows: