Javascript Reference - HTML DOM Button formAction Property








The formAction property sets or gets the value of the formaction attribute of a button.

The formaction attribute specifies where to send the form-data when a form is submitted.

This attribute overrides the HTML form's action attribute.

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

The formaction attribute is new for the <button> element in HTML5.

Browser Support

formAction Yes 10 Yes Yes Yes

Syntax

Return the formAction property:

var v = buttonObject.formAction 

Set the formAction property:

buttonObject.formAction=URL




Property Values

Value Description
URL Specifies where to send the form-data.

Return Value

A String representing where to send the form-data.

Example

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


<!DOCTYPE html>
<html>
<body>
<form action="url" method="get">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" type="submit" formaction="another url">Submit</button>
</form><!--   ww w .j  a v  a  2  s .c  o  m-->

<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myBtn").formAction;
    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 value of the formaction attribute of a button.


<!DOCTYPE html>
<html>
<body>
<form action="url" method="get">
First name: <input type="text" name="fname" value="A"><br>
Last name: <input type="text" name="lname" value="D"><br>
<button id="myBtn" type="submit" formaction="a new url">Submit form</button>
</form><!--  ww  w  .ja  va  2 s .  c om-->

<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    document.getElementById("myBtn").formAction = "a another new url";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: