Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i want a anchor should act like and input type submit button

i want a anchor should act like and input type submit button. i am using a jquery plugin library that actually uses input type submit but i have styled my buttons on anchors. i dont 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 button.

like image 726
Mossawir Ahmed Avatar asked May 22 '11 08:05

Mossawir Ahmed


2 Answers

If you want an anchor tag to act like a button just do this

<!--YOUR FORM-->
<form id="submit_this">.....</form>
<a id="fakeanchor" href="#"></a>

<script>
    $("a#fakeanchor").click(function()
    {
    $("#submit_this").submit();
    return false;
    });
</script>
like image 81
mazlix Avatar answered Nov 08 '22 16:11

mazlix


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

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

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


Off-topic: But note that this makes your page completely unusable without JavaScript, as well as making it confusing even for JavaScript-enabled browsers employed by users requiring assistive technologies (screenreaders, etc.). It makes the markup completely un-semantic. But since you'd said quite clearly that this was what you wanted to do...

like image 45
T.J. Crowder Avatar answered Nov 08 '22 15:11

T.J. Crowder