Form action Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Form

Description

The action property sets or gets the action attribute in a form, which controls where to send the form data when a form is submitted.

Set the action property with the following Values

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

Possible values:

  • An absolute URL - like action="http://www.example.com/example.htm"
  • A relative URL - like action="example.htm"

Return Value

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

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="nothing.asp">
  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>// w  ww  . ja  v a  2s .  c o m

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

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

<script>
function myFunction() {
    document.getElementById("myForm").action = "#";
    document.getElementById("demo").innerHTML = "The value of the action attribute was changed to #.";
}
</script>

</body>
</html>

Related Tutorials