a tag as a submit button?

theepan picture theepan · Sep 7, 2011 · Viewed 118.1k times · Source

Hi I am trying to get "a" tag as a submit button. I found a code somewhere in the web. But it didn't work.

<a href="#" onclick="this.form.submit()">Submit</a>

Is there any code for to achieve my need?

Answer

James Allardice picture James Allardice · Sep 7, 2011

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();
}