Instead of a submit button I have a link:
<form>
<a href="#"> submit </a>
</form>
Can I make it submit the form when it is clicked?
Instead of a submit button I have a link:
<form>
<a href="#"> submit </a>
</form>
Can I make it submit the form when it is clicked?
If you use jQuery and would need an inline solution, this would work very well;
Also, you might want to replace
with
so the user does not scroll to the top of your page when clicking the link.
You could give the form and the link some ids and then subscribe for the
onclick
event of the link andsubmit
the form:and then:
I would recommend you using a submit button for submitting forms as it respects the markup semantics and it will work even for users with javascript disabled.
here's the best solution to your question: inside the form you have these code:
in the CSS file, do this:
in JS you do this:
There you go.
The best way
The best way is to insert an appropriate input tag:
The best JS way
Enclose the latter JavaScript code by an
DOMContentLoaded
event (choose onlyload
for backward compatiblity) if you haven't already done so:The easy, not recommandable way (the former answer)
Add an
onclick
attribute to the link and anid
to the form:All ways
Whatever way you choose, you have call
formObject.submit()
eventually (whereformObject
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.
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.
Now, we come to the point at which you have to decide for the UI element which triggers the submit() call.
A button
A link
Apply the aforementioned techniques in order to add an event listener.
HTML & CSS - No Javascript Solution
HTML:
CSS: