[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

Having more than one submit_tag call the same javascript function

Hello,

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" %>

  $j('#call_js_function').click(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,
Tim
0
tmitch68
Asked:
tmitch68
  • 2
1 Solution
 
Pratima PharandeCommented:
can try

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

You can Use button_to_function

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

http://stackoverflow.com/questions/7510260/submit-tag-with-javascript-function
0
 
tmitch68Author Commented:
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now