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.
🌐
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; }
🌐
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.
🌐
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.
🌐
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.
🌐
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 › 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.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › form
The Form element - HTML - MDN Web Docs - Mozilla
<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!"
🌐
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 ...
🌐
React
react.dev › reference › react-dom › components › form
form
... 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.
🌐
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.
🌐
CodePen
codepen.io › prasanthmj › pen › WNbgGav
Form action using javascript function
Minimize JavaScript Editor · Fold All · Unfold All · function setAction(form) { form.action = "register.html"; alert(form.action); return false; } function loginUser() { document.user_form.action = "login.html"; alert(document.user_form.action); return false; } function registerUser() { document.user_form.action = "register.html"; alert(document.user_form.action); return false; } !
🌐
Delft Stack
delftstack.com › home › howto › javascript › form action javascript
Form Action Attribute in JavaScript | Delft Stack
July 4, 2025 - This article explains JavaScript’s form action attribute. It accesses the form, gets values of all fields, validates form data, and sends it to the right destination.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript dom › javascript form
JavaScript Form
December 17, 2023 - Summary: in this tutorial, you will learn about JavaScript form API: accessing the form, getting values of the elements, validating form data, and submitting the form. To create a form in HTML, you use the <form> element: <form action="/signup" method="post" id="signup"> </form>Code language: HTML, XML (xml)
🌐
InfoWorld
infoworld.com › home › software development › programming languages › javascript
Using JavaScript and forms | InfoWorld
January 12, 2023 - When handled by the browser’s default behavior, the action field is a URL telling the browser where to send the data. When we take control of the data with JavaScript and Ajax, we manually specify where we are going to send the info (one way is by using the data field in with a fetch call, which I’ll demonstrate shortly). Sometimes, you’ll see the URL set on the form, and then the JavaScript will programmatically pull the value from the form action field to use as a destination for an Ajax request.
🌐
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.