Submitting a form from an a href link

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 it entering a renaissance of sorts, 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?
Here’s one way I’ve learned:

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

“this” presumably refers to the a href tag. You then call submit() on its parent form. Pretty cool.


About buffalobillion

Web Developer, JavaScript Balrog, Java dude, Ruby/Rails enthusiast. Guitar Playa.
This entry was posted in Web Dev. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s