Francis Smart
Francis Smart

Reputation: 4055

Make link submit identically to default button behavior

Sorry if this seems redundant. I have read through quite a few posts and cannot seem to figure out how to accomplish this. I need an element that behaves like a button but I thought to use a css class object instead. I have found a number of references showing how to do this by making href="#" and adding javascript command.

<p><input name="Test" type="Button" value="Click" /></p>

However, due to the server side software I am using I need to exactly duplicate the behavior of the button in order to make it submit my form.

I am guessing it is something obvious like:

<p><a class="button" href="#" onclick="submit()">Click</a></p>

Thanks for any help you can provide!

Upvotes: 0

Views: 113

Answers (3)

Arbel
Arbel

Reputation: 30999

This will do the job:

<a class="button" href="#" onclick="document.forms[0].submit();">Click</a>

If you have one form in the document, else change 0 accordingly.

OR

<a class="button" href="#" onclick="document.forms['formName'].submit();">Click</a>

Uses form name.

OR

<a class="button" href="#" onclick="document.getElementById('formID').submit();">Click</a>

Uses form id.

Upvotes: 1

LcSalazar
LcSalazar

Reputation: 16841

You have to reference to the form element, and then call the submit:

<p><a class="button" href="#" onclick="document.getElementById('myFormId').submit()">Click</a></p>

Though you should not be using inline event delegation:

HTML:

<p><a class="button" href="#" id="submit-anchor">Click</a></p>

JS:

document.getElementById("submit-anchor").addEventListener("click", function() {
    document.getElementById('myFormId').submit();
});

Upvotes: 1

Birgit Martinelle
Birgit Martinelle

Reputation: 1889

you have to use "form.submit();" you should check out this question: Use a normal link to submit a form

Upvotes: 0

Related Questions