Solved

Having more than one submit_tag call the same javascript function

Posted on 2012-12-30
3
805 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 500 total points
ID: 38731961
0
 

Author Closing Comment

by:tmitch68
ID: 38731974
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

691 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