Absolutely valid.

    <form action="javascript:alert('Hello there, I am being submitted');">
        <button type="submit">
            Let's do it
        </button>
    </form>
    <!-- Tested in Firefox, Chrome, Edge and Safari -->

So for a short answer: yes, this is an option, and a nice one. It says "when submitted, please don't go anywhere, just run this script" - quite to the point.

But ... !

Why don't we take this to the next level - let the event handler know which form we're dealing with! Here's how to do it. (Note how the first form, no matter how obvious, won't do the job. We need a few keystrokes more.)

    <form action="javascript: myFunction( this ) ">  
    <!-- But this won't work -->

    <form action="javascript:;" onsubmit=" myFunction( this ) ">  
    <!-- Tadaammm! -->

Now you can access the sender form properly. (You can write a simple "#" as action, it's quite common - but it has a side effect of scrolling to the top when submitting.)

Again, I like this approach because it's effortless and self-explaining. No "return false", no jQuery/domReady, no heavy weapons. It just does what it seems to do. Surely other methods work too, but for me, this is The Way Of The Samurai.

A note on validation

Forms only get submitted if their onsubmit event handler returns something truthy, so you can easily run some preemptive checks:

    <form action="/something.php" onsubmit=" return isMyFormValid ( this ) ">

Now isMyFormValid will run first, and if it returns false, server won't even be bothered. Needless to say, you will have to validate on server side too, and that's the more important one. But for quick and convenient early detection this is fine.

Answer from dkellner on Stack Overflow
Top answer
1 of 5
63

Absolutely valid.

    <form action="javascript:alert('Hello there, I am being submitted');">
        <button type="submit">
            Let's do it
        </button>
    </form>
    <!-- Tested in Firefox, Chrome, Edge and Safari -->

So for a short answer: yes, this is an option, and a nice one. It says "when submitted, please don't go anywhere, just run this script" - quite to the point.

But ... !

Why don't we take this to the next level - let the event handler know which form we're dealing with! Here's how to do it. (Note how the first form, no matter how obvious, won't do the job. We need a few keystrokes more.)

    <form action="javascript: myFunction( this ) ">  
    <!-- But this won't work -->

    <form action="javascript:;" onsubmit=" myFunction( this ) ">  
    <!-- Tadaammm! -->

Now you can access the sender form properly. (You can write a simple "#" as action, it's quite common - but it has a side effect of scrolling to the top when submitting.)

Again, I like this approach because it's effortless and self-explaining. No "return false", no jQuery/domReady, no heavy weapons. It just does what it seems to do. Surely other methods work too, but for me, this is The Way Of The Samurai.

A note on validation

Forms only get submitted if their onsubmit event handler returns something truthy, so you can easily run some preemptive checks:

    <form action="/something.php" onsubmit=" return isMyFormValid ( this ) ">

Now isMyFormValid will run first, and if it returns false, server won't even be bothered. Needless to say, you will have to validate on server side too, and that's the more important one. But for quick and convenient early detection this is fine.

2 of 5
58

A form action set to a JavaScript function is not widely supported, I'm surprised it works in FireFox.

The best is to just set form action to your PHP script; if you need to do anything before submission you can just add to onsubmit

Edit turned out you didn't need any extra function, just a small change here:

function validateFormOnSubmit(theForm) {
    var reason = "";
    reason += validateName(theForm.name);
    reason += validatePhone(theForm.phone);
    reason += validateEmail(theForm.emaile);

    if (reason != "") {
        alert("Some fields need correction:\n" + reason);
    } else {
        simpleCart.checkout();
    }
    return false;
}

Then in your form:

<form action="#" onsubmit="return validateFormOnSubmit(this);">
🌐
W3Schools
w3schools.com › jsref › prop_form_action.asp
HTML DOM Form action Property
The action property sets or returns the value of the action attribute in a form.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › form
<form>: The Form element - HTML | MDN
<form action="" method="get" class="form-example"> <div class="form-example"> <label for="name">Enter your name: </label> <input type="text" name="name" id="name" required /> </div> <div class="form-example"> <label for="email">Enter your email: </label> <input type="email" name="email" id="email" required /> </div> <div class="form-example"> <input type="submit" value="Subscribe!"
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Extensions › Forms › Sending_and_retrieving_form_data
Sending form data - Learn web development | MDN
The action attribute defines where the data gets sent. Its value must be a valid relative or absolute URL. If this attribute isn't provided, the data will be sent to the URL of the page containing the form — the current page.
🌐
Reddit
reddit.com › r/javascript › html form, get action, call javascript
r/javascript on Reddit: html form, get action, call javascript
December 18, 2013 -

Ok, So I've read a lot of approaches on stack overflow, but they don't address the issue. They are all for one time use and you have to label and build extensive javascript functions to do something very simple.

I have a very clean HTML5 form that uses the new validation and is great. I have it use a get to a php action and it works perfect.

However I'm trying to improve user experience so I've written a javascript function to do an ajax call to that php action. The problem is I want it to be generic. I've got dynamic form values and I want to use the same function for all my forms. Really I want the form submit to just act like it normally would and append the get string to the end of whatever url I pass. I can't get the form to submit the get values to the javascript function.

This works:

<form action="myphp.php" method="get">
    <label>Month:</label><input id="day" type="number" min="1" max="12" name="week" value="" required> <br>
    <label>Year:</label><input id="year" type="number" min="2008" max="2030" name="year" value="" required><br>
    <input id="cmdReset" type="submit" value="What month is it?">
 </form>

This also works, kind of:

<form action="javascript:displayPostResult('myphp.php')" method="get">

It calls the page and displays the results on the page in an awesome ajaxy way just like I want it to, I just can't pass the form's get values.

I'd like to use that displayPostResult function on about 20 pages with all kinds of different form data so creating individual functions to handle each page is out of the question. I really just want to append the get values as if the form was submitted to the end of the string being passed to the function. I just can't find a way to do that.

Is there any way I can pass "?month=12&year=2013" to the javascript function?

i.e. displayPostResult('myphp.php', <get parameters>)

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Extensions › Forms › Your_first_form
Your first form - Learn web development | MDN
A click on a submit button (the default value) sends the form's data to the web page defined by the action attribute of the <form> element.
Find elsewhere
🌐
Formspree
formspree.io › blog › html-form-action
A Mini Guide to HTML Form Action | Formspree
February 27, 2025 - In some scenarios, you may need to set or modify the form action dynamically based on user input or conditions. This can be achieved using JavaScript, allowing your HTML form to handle multiple submission endpoints.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-find-the-action-attribute-and-method-of-a-form-in-javascript
How to Find the action Attribute and method of a Form in JavaScript?
2 weeks ago - <!DOCTYPE html> <html> <head> <title>Getting Form Action Attribute</title> </head> <body> <form id="myForm" action="/submit-form" method="post"> <label>Full name: <input type="text" name="fname"></label><br><br> <label>Email: <input type="email" name="email"></label><br><br> <input type="submit" value="Submit"> </form> <p id="result"></p> <script> const form = document.getElementById('myForm'); document.getElementById('result').innerHTML = 'Form action: ' + form.action; </script> </body> </html>
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLInputElement › formAction
HTMLInputElement: formAction property - Web APIs | MDN
April 10, 2025 - The formAction property of the HTMLInputElement interface is the URL of the program that is executed on the server when the form that owns this control is submitted. It reflects the value of the <input>'s formaction attribute. This property is valid only for submit and image <input> elements.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-submit-a-form-with-javascript
How to Submit a Form with JavaScript – JS Submit Button Example
November 7, 2024 - In this article, you have learned how to submit a form with JavaScript and how it works with the various DOM methods.
🌐
DotFactory
dofactory.com › html › form › action
HTML form action
The action attribute specifies where to send the form data when submitted. Form data is mostly submitted to a server-side handler, but it can also be JavaScript on the client.
🌐
HTML Form Guide
html.form.guide › html-form › form-action-using-javascript-function
Setting the Form Action with a JavaScript Function | HTML Form Guide
Add the following JavaScript to the file. function setAction(form) { form.action = "register.html"; alert(form.action); return false; }
🌐
The Coding Forums
thecodingforums.com › archive › archive › javascript
questions about form action= and onsubmit() handling | Javascript | Coding Forums
January 5, 2009 - Hi William Gill, The action attribute of a form is supposed to receive an URL or "", not a function. According to the excellent book Javascript - the definitive guide 4th edition: "if you specify a javascript: URL as the value of the action attribute of a <form> tag, the JavaScript code in the URL is executed when the user submits the form...
🌐
SitePoint
sitepoint.com › javascript
Dynamically set the action of a form - JavaScript - SitePoint Forums | Web Development & Design Community
July 8, 2011 - Hi As a Javascript Noob I’m having a bit of problem writing some javascript that will dynamically update the ‘action’ of a form when the form is submitted. Its for an Image Upload script which performs a binary read on the uploaded image, meaning I have to POST the form, but must pass ...
🌐
W3Schools
w3schools.com › tags › att_form_action.asp
HTML form action Attribute
The action attribute specifies where to send the form-data when a form is submitted. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report ...
🌐
GeeksforGeeks
geeksforgeeks.org › html › html-dom-form-action-property
HTML DOM Form action Property - GeeksforGeeks
July 11, 2025 - The HTML DOM Form action property allows accessing and modifying the action attribute of a form element through JavaScript.
🌐
React
react.dev › reference › react-dom › components › form
<form> – React
... action: a URL or function. When a URL is passed to action the form will behave like the HTML form component. When a function is passed to action the function will handle the form submission in a Transition following the Action prop pattern.
🌐
WS Form
wsform.com › home › knowledge base › run javascript action
The Run JavaScript Action
June 12, 2025 - The Run JavaScript action allows you to run any JavaScript when a form is saved or submitted.
🌐
W3Schools
w3schools.com › jsref › prop_submit_formaction.asp
HTML DOM Input Submit formAction Property
The formaction attribute overides the action attribute of the <form> element.