How to submit a form with JavaScript by clicking a link?
The best way
The best way is to insert an appropriate input tag:
<input type="submit" value="submit" />
The best JS way
<form id="form-id">
<button id="your-id">submit</button>
</form>
var form = document.getElementById("form-id");
document.getElementById("your-id").addEventListener("click", function () {
form.submit();
});
Enclose the latter JavaScript code by an DOMContentLoaded
event (choose only load
for backward compatiblity) if you haven't already done so:
window.addEventListener("DOMContentLoaded", function () {
var form = document.... // copy the last code block!
});
The easy, not recommandable way (the former answer)
Add an onclick
attribute to the link and an id
to the form:
<form id="form-id">
<a href="#" onclick="document.getElementById('form-id').submit();"> submit </a>
</form>
All ways
Whatever way you choose, you have call formObject.submit()
eventually (where formObject
is the DOM object of the <form>
tag).
You also have to bind such an event handler, which calls formObject.submit()
, so it gets called when the user clicked a specific link or button. There are two ways:
Recommended: Bind an event listener to the DOM object.
// 1. Acquire a reference to our <form>.
// This can also be done by setting <form name="blub">:
// var form = document.forms.blub;
var form = document.getElementById("form-id");
// 2. Get a reference to our preferred element (link/button, see below) and
// add an event listener for the "click" event.
document.getElementById("your-id").addEventListener("click", function () {
form.submit();
});Not recommended: Insert inline JavaScript. There are several reasons why this technique is not recommendable. One major argument is that you mix markup (HTML) with scripts (JS). The code becomes unorganized and rather unmaintainable.
<a href="#" onclick="document.getElementById('form-id').submit();">submit</a>
<button onclick="document.getElementById('form-id').submit();">submit</button>
Now, we come to the point at which you have to decide for the UI element which triggers the submit() call.
A button
<button>submit</button>
A link
<a href="#">submit</a>
Apply the aforementioned techniques in order to add an event listener.
How to make a submit out of a a href... ... /a link?
<input type="image" name="your_image_name" src="your_image_url.png" />
This will send the your_image_name.x
and your_image_name.y
values as it submits the form, which are the x and y coordinates of the position the user clicked the image.
a tag as a submit button?
Give the form
an id
, and then:
document.getElementById("yourFormId").submit();
Best practice would probably be to give your link an id
too, and get rid of the event handler:
document.getElementById("yourLinkId").onclick = function() {
document.getElementById("yourFormId").submit();
}
Make a link use POST instead of GET
You create a form with hidden inputs that hold the values to be posted, set the action of the form to the destination url, and the form method to post. Then, when your link is clicked, trigger a JS function that submits the form.
See here, for an example. This example uses pure JavaScript, with no jQuery — you could choose this if you don't want to install anything more than you already have.
<form name="myform" action="handle-data.php" method="post">
<label for="query">Search:</label>
<input type="text" name="query" id="query"/>
<button>Search</button>
</form>
<script>
var button = document.querySelector('form[name="myform"] > button');
button.addEventListener(function() {
document.querySelector("form[name="myform"]").submit();
});
</script>
JavaScript post request like a form submit
Dynamically create <input>
s in a form and submit it
/**
* sends a request to the specified url from a form. this will change the window location.
* @param {string} path the path to send the post request to
* @param {object} params the parameters to add to the url
* @param {string} [method=post] the method to use on the form
*/
function post(path, params, method='post') {
// The rest of this code assumes you are not using a library.
// It can be made less verbose if you use one.
const form = document.createElement('form');
form.method = method;
form.action = path;
for (const key in params) {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.type = 'hidden';
hiddenField.name = key;
hiddenField.value = params[key];
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
Example:
post('/contact/', {name: 'Johnny Bravo'});
EDIT: Since this has gotten upvoted so much, I'm guessing people will be copy-pasting this a lot. So I added the hasOwnProperty
check to fix any inadvertent bugs.
How can I submit a form using JavaScript?
Set the name
attribute of your form to "theForm"
and your code will work.
Use a normal link to submit a form
Two ways. Either create a button and style it so it looks like a link with css, or create a link and use onclick="this.closest('form').submit();return false;"
.
Related Topics
HTML Email - Is Colspan Allowed
Nesting <A> Inside <Button> Doesn't Work in Firefox
Text-Align Class for Inside a Table
Why Is the Parent Div Height Zero When It Has Floated Children
Iframes and the Safari on the iPad, How Can the User Scroll the Content
Send Email from Static Page Hosted on Github Pages
Next.Js Redirect From/To Another Page
How to Add a "Search" Button in a Text Input Field
What Character Encoding Should I Use for a Web Page Containing Mostly Arabic Text? Is Utf-8 Okay
How to Make Div Element Auto-Resize Maintaining Aspect Ratio
Link with Target="_Blank" and Rel="Noopener Noreferrer" Still Vulnerable
Jenkins - HTML Publisher Plugin - No CSS Is Displayed When Report Is Viewed in Jenkins Server
Xslt: Moving a Grouping HTML Elements into Section Levels
I Have Position But Z Index Is Not Working
How to Auto-Format/Indent Xml/HTML in Notepad++
How to Change the Length/Position of Text-Decoration:Underline