0

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 :/

user700304
  • 45
  • 4

2 Answers2

0

I would suggest that you keep your submit button then add a event to it then prevent the submit I'm assuming that you want to make an ajax call when you submit ?

the html

 <input type="submit" class="submit" />

the jQuery

$('.submit').click(function(event){
     event.preventDefault();
    //do what you want here
 });
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
0

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;
});
zeeraw
  • 5,197
  • 1
  • 21
  • 27
  • yes, but how do i replace the input:submit button with a link_to_function when js is turned on? I still want to show the input:submit button when js is turned off. – user700304 Apr 10 '11 at 08:07
  • You could make your template spawn both a submit button and submit link and make sure the link is by default hidden through CSS. And if JS is activated you can use JS to hide the button and show the link when the DOM is loaded. – zeeraw Feb 14 '12 at 14:08