I want the anchor to act like an input type input button

I want the anchor to act as an input button for an input type. I use the jquery plugin library which actually uses the input type submit, but I created my buttons on the anchors. I do not want to use

<input type="button"> 

or

 <input type="submit"> 

I want to use anchors such as

 <a href="javascript to submit the form" ></a> 

and here is my jquery code where I want to use

  { var submit = $('<button type="submit" />'); submit.html(settings.submit); } $(this).append(submit); } if (settings.cancel) { /* if given html string use that */ if (settings.cancel.match(/>$/)) { var cancel = $(settings.cancel); /* otherwise use button with given string as text */ } else { var cancel = $('<button type="cancel" />'); 

how to use anchors instead of buttons.

+4
source share
5 answers

If you want the anchor tag to act like a button, just do it

 <!--YOUR FORM--> <form id="submit_this">.....</form> <a id="fakeanchor" href="#"></a> <script> $("a#fakeanchor").click(function() { $("#submit_this").submit(); return false; }); </script> 
+15
source

Since you are using jQuery, just use $() to select the form element and call submit on it; move all this to the anchor via $() to find the anchor and click to connect the handler:

 $("selector_for_the_anchor").click(function() { $("selector_for_the_form").submit(); return false; }); 

Probably best is return false; cancel click on the anchor.


Off topic . But note that this makes your page completely unusable without JavaScript, and also makes it confusing even for JavaScript-enabled browsers used by users who need assistive technologies (firmware, etc.). This makes markup completely meaningless. . But since you clearly said that this is exactly what you wanted to do ...

+3
source
 <a id='anchor' href="javascript to submit the form" ></a> 

you can now use jquery to add an event handler

 $('#anchor').click(function (e) { // do some work // prevent the default anchor behaviour e.preventDefault(); }) 

Now you can create your anchor as you want, and it will act like a regular button

+2
source

What about:

 <form id="formOne"> ... <a href="..." onclick="formOne.submit()">link here</a> </form> 
+2
source

you can use image type input (it works like a submit button for a form) or in jquery:

 $("a").click(function(event){ event.preventDefault(); $('form').submit(); }) 
+1
source

All Articles