Solved

Having more than one submit_tag call the same javascript function

Posted on 2012-12-30
3
774 Views
Last Modified: 2012-12-30
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
Comment
Question by:tmitch68
  • 2
3 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38731959
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
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 38731961
0
 

Author Closing Comment

by:tmitch68
ID: 38731974
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now