Submitting a form from an a href link using jquery

posted by Ted on 20 Jul 2010

Okay, Okay, I admit it. I'm javascript challanged. As a Java developer, I've looked down my nose at paltry javascript, treating it as a "me-too" technology trying to cash in on the hype surrounding java all these years.
As a result, I've missed out on a lot of js goodies. And, with javascript entering a renaissance of sorts, combined with the wildfire-like buzz surrounding libraries such as jquery, it's high time I learn some.

One of the things I've had to work around are these hoity-toity designers who don't like to use typical "submit" buttons for form submissions when they're designing forms. They like to use anchor tags, presumably because they're more easily styleable. So, how does one submit a form without a submit button using jquery?

Here's one way I've learned:

<form>
  // Arbitrary code here
  <a href="#" onclick="$(this).parents('form').submit();">Your text</a>
  // More arbitrary code here
</form>

blog comments powered by Disqus