157

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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
sandy
  • 1,601
  • 2
  • 11
  • 4
  • I use this to integrate a completely hidden form into a page. But there are line breaks before and after the link. Do they come from the form element? And I would like to open the link in a new tab/page, `target="_blank"` does not seem to work. – JPT Nov 27 '15 at 13:29
  • 3
    **tl;dr** `Submit` – vhs Mar 11 '17 at 14:19

9 Answers9

258

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.

  1. A button

    <button>submit</button>
    
  2. A link

    <a href="#">submit</a>
    

Apply the aforementioned techniques in order to add an event listener.

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
77

If you use jQuery and would need an inline solution, this would work very well;

<a href="#" onclick="$(this).closest('form').submit();">submit form</a>

Also, you might want to replace

<a href="#">text</a>

with

<a href="javascript:void(0);">text</a>

so the user does not scroll to the top of your page when clicking the link.

Maurice
  • 4,829
  • 7
  • 41
  • 50
  • 1
    I believe it would be preferable to make it in a text. It's not a link, and href:javascript is not good. (soon to become bad with next security reasons ... inlined javascript is to be banned someday. – Milche Patern Nov 06 '13 at 23:08
  • I agree. Judging from the nature of the question I went for the simple answer. What I personally do is use `href="#"` and then select all those links with jquery and call `e.preventDefault()` in the `click` event handler so the browser doesn't scroll. – Maurice Mar 20 '14 at 14:44
  • 3
    I personally prefer this answer. Simple and clean and easy to implement if your page is using JQuery. Very flexible too, as I have a page with multiple forms on it and it works flawlessly. – John Contarino Oct 20 '15 at 20:53
  • 1
    One can use href="#" if one adds at the end of the onClick javascript call "return false" – Lumis Oct 31 '19 at 13:02
26

You could give the form and the link some ids and then subscribe for the onclick event of the link and submit the form:

<form id="myform" action="" method="POST">
    <a href="#" id="mylink"> submit </a>
</form>

and then:

window.onload = function() {
    document.getElementById('mylink').onclick = function() {
        document.getElementById('myform').submit();
        return false;
    };
};

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
6

HTML & CSS - No Javascript Solution

Make your button appear like a Bootstrap link

HTML:

<form>
    <button class="btn-link">Submit</button>
</form>

CSS:

.btn-link {

    background: none;
    border: none;
    padding: 0px;
    color: #3097D1;
    font: inherit;

}

.btn-link:hover {

    color: #216a94;
    text-decoration: underline;

}
Darren Murphy
  • 1,076
  • 14
  • 12
  • 1
    Excellent, why make it complicated when it can be simple. However, I think it looks even better if you also add "cursor: pointer;" in the hover class, so the cursor turns into the hand. – Lumis Oct 31 '19 at 12:44
5

this works well without any special function needed. Much easier to write with php as well. <input onclick="this.form.submit()"/>

chris
  • 51
  • 1
  • 1
5

My suggestion is:

<form action="/logout" method="POST">
    <button type="submit" style="display:none;">Log Out</button>
    <a href="/logout" onclick="event.preventDefault();this.closest('form').submit();">Log Out</a>
</form>
Adam Pery
  • 1,924
  • 22
  • 21
3
<form id="mailajob" method="post" action="emailthijob.php">
    <input type="hidden" name="action" value="emailjob" />
    <input type="hidden" name="jid" value="<?php echo $jobid; ?>" />
</form>

<a class="emailjob" onclick="document.getElementById('mailajob').submit();">Email this job</a>
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Shashank
  • 41
  • 1
  • 3
    Can you explain how your answer actually answers the question? Add more context to it please. –  Oct 17 '14 at 08:38
2
document.getElementById("theForm").submit();

It works perfect in my case.

you can use it in function also like,

function submitForm()
{
     document.getElementById("theForm").submit();
} 

Set "theForm" as your form ID. It's done.

Chintan Thummar
  • 1,462
  • 19
  • 32
0

here's the best solution to your question: inside the form you have these code:

<li><a href="#">Singup</a></li>
<button type="submit" id="haha"></button>

in the CSS file, do this:

button{
  display: none;
}

in JS you do this:

$("a").click(function(){
   $("button").trigger("click");
})

There you go.

iamwave007
  • 333
  • 2
  • 3
  • 10