11

I am new to rails.I want to call a javascript function when click the submit button.I used submit_tag , but the function did not get triggered.I want some thing like the following ,

     <%= submit_tag , :onsubmit => "return validateform()" %>

I googled the problem but I couldn't find a solution.Please any one give a solution.

karthi_ms
  • 5,568
  • 11
  • 38
  • 39

4 Answers4

24

try onclick

<%= submit_tag , :onclick => "return validateform();" %>

or if you want to use onsubmit you can use that in your form_tag.

Mahesh
  • 6,378
  • 2
  • 26
  • 35
7

You can use something like this on the form tag

 <% form_for :bla, :html => { :onsubmit => 'blahblah;' } do |f| %>
nateleavitt
  • 1,210
  • 10
  • 9
5

You can try

<%= submit_tag , :onclick => "return validateform()" %>

OR

You can Use button_to_function

<%= button_to_function "Greeting", "validateform()" %>

And in your Javascript function, if all the validations pass, submit the form.

Chip Roberson
  • 532
  • 3
  • 12
Salil
  • 46,566
  • 21
  • 122
  • 156
3

You would do it in the form_tag...

<%= form_tag({:action => :whatever}, {:onSubmit => 'return validateform();'} ) %>

Michael Frederick
  • 16,664
  • 3
  • 43
  • 58