Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to make a link submit a form

What's the best way to get a regular anchor (<a href="...">) to submit the form it is embedded in when clicked?

<form>     <ul>         <li>             <p>                 The link could be <span>embedded <a href="" onclick="?">at any level</a></span>                 in the form, so "this.parentNode.parentNode..." is no good. :(             </p>         </li>     </ul> </form> 

I know that the easiest way using jQuery would be

$('#myLink').click(function() {     $(this).parents('form:first').submit(); }); 

...but I'm trying to find a way to do this without using a library.


Edit: I'm really trying to find a method which doesn't require knowledge of the form (eg: its name, id, etc). This would be similar to how you could put this on an input element: <input onclick="this.form.submit()" />

like image 623
nickf Avatar asked Nov 24 '08 05:11

nickf


People also ask

How do I submit a form through a link?

In this article, we have submitted a form using JavaScript by clicking a link. In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked.

How do you submit a form on click?

To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.

What is the correct way to add a submit URL to a button element?

The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute. If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit"> .

How do I link a button to another page in HTML?

In HTML, a button link to another page can be by using the <a> tag, <input> tag, and the <form> tag. A link on a button is get by href=”” attribute of <a> tag. The “type=button” and “onclick=link” attributes are used to create a link on the button.


1 Answers

Why don't you use an <input> or <button> element and just tweak it with CSS? Then it works without Javascript and is therefore more reliable.

like image 130
ypnos Avatar answered Sep 19 '22 23:09

ypnos