Techblog

Tech Blog

Our latest geek adventures!

22 March Form_remote_tag submit by Javascript

Posted by jaap on in Javascript, Ruby on Rails

When you try to submit a form from Javascript, using form_remote_tag, like this:

Rails:

form_remote_tag :url => {:controller => "somecontroller", :action => "} , :html => {:id => "ajax-form-1">

If you just invoke form.submit(), like this:

var form = document.getElementById('ajax-form-1');
if(form)
   form.submit();

the form will be submitted to a new page, that’s not soo ajaxy you now think. Instead use:

var form = document.getElementById('ajax-form-1');<br />
if(form.onsubmit()){
   form.submit();
}

Now your form will be submitted on the AJAX way!

Tags: ,

6 Responses to “Form_remote_tag submit by Javascript”

  1. r Says:

    Great! Thanks!

  2. Raph Says:

    Thanks, it’s of great help!

  3. Ian Terrell Says:

    For JQuery users (with jRails), calling onsubmit() on the form won’t work. Instead, call trigger(’onsubmit’).

  4. feng Says:

    thanks, by chinese rails coder

  5. Raj Says:

    Thanks for this info.
    I was stumped why my page refreshed even after using form_remote_tag.
    Now I know!

  6. Raghav Dasson Says:

    Thanks!!! It was of great use to me.

Leave a Reply