?
Solved

Having more than one submit_tag call the same javascript function

Posted on 2012-12-30
3
Medium Priority
?
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 38731961
0
 

Author Closing Comment

by:tmitch68
ID: 38731974
Thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

762 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