Topic: AJAX Form with a link to submit?


This has been really bugging me sad Is it possible to submit an AJAX form via a text link? I don't want the big <input type="submit"> button!

Here's some code:

<div id="login">
<%= form_remote_tag(:update => "login", :url => { :controller => "user", :action => :login }) %>
<%= text_field_tag("email", "email", :maxlength=>500, :class=>"loginElement") %>
<%= password_field_tag("password", "password", :class=>"loginElement") %>
<a href="javascript:document.forms[0].submit()">Login</a>
<%= end_form_tag %>

As you can see, I've tried document.forms[0].submit(), but it just sends the form off like normal, instead of AJAXing it...

Any ideas? Thank you!


Re: AJAX Form with a link to submit?

IIRC, try this:


I think this will trigger the onsubmit attribute on the form so it will trigger the javascript portion of the form. That's just from memory though - I know it's something similar if that doesn't do it.

Railscasts - Free Ruby on Rails Screencasts

Re: AJAX Form with a link to submit?

Thank you!

I actually had to do the following:

if(document.forms[0].onsubmit()) {

But I would never have found that if you hadn't told me about .onsubmit

Thank you!