Search code examples
jqueryruby-on-railslink-to-function

replace input button with jquery


I'm trying to replace a Submit button in a form with a javascript function. I'm able to insert the following link_to_function for submitting the form:

  <%= link_to_function 'Create', "$('form').submit()" %>

Now, I want to replace the default form submit button with the js function above with jquery to have a fallback option when js is disabled. The question is now:

how do I replace the ordinary submit button inside my application.js file?

I tried:

  $('input:submit').replaceWith("<%= escape_javascript(link_to_function 'Create', "$('form').submit()") %>");

which doesn't seem to work :/


Solution

  • In my application I do something similar - I have a button which performs the submit action on any present form.

    the JQuery:

    $('#save').click(function(){
        $('form').submit();
        return false;
    });