Having more than one submit_tag call the same javascript function

tmitch68 used Ask the Experts™

I have a Ruby-on-Rails web application in which I need to have more than one submit_tag (or any type of button) call the same function - WITHOUT having to do an actual submit.

I can do this with one submit_tag like so...

<% submit_tag "Call JS Function", :id => "call_js_function" %>

    //Do stuff - function is indeed called
    return false;

However, this doesn't work with more than one submit_tag - since I can't give two submit_tags the same ID.

So, I though maybe onclick might work.  So far, I've seen dozens of examples of how to user onclick; none of them have actually worked - although maybe I'm just not using it correctly.

I create my javascript function like so...

$j(function javascript_function()
  //try to do stuff here - but it never gets executed
  return false;

And I try to call it using the onclick attribute.  However, none of these seem to work:

<%= submit_tag "Call JS Function", :onclick => "$j(javascript_function())" %>
<%= submit_tag "Call JS Function", :onclick => "javascript_function()" %>
<input type='submit' value='Call JS Function' onclick='javascript_function()'/>

So far, none of these work.

Any suggestions?

Thanks in advance,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
can try

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

You can Use button_to_function

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




Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial