# Called javascript method and ruby method with 1 button?

**URL:** <https://www.ruby-forum.com/t/called-javascript-method-and-ruby-method-with-1-button/92645>\
**Category:** Rails\
**Created:** [April 22, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/called-javascript-method-and-ruby-method-with-1-button/92645 "2007-04-22T12:06:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [April 22, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/called-javascript-method-and-ruby-method-with-1-button/92645/1 "2007-04-22T12:06:04Z")

</div>

Hi

I create a javascript method in rhtml with   
and I would like to have 1 button that will call my controller method  
and js() at the same time, so user will only need to click 1 button  
instead of 2

Is this possible to do?

Thank you

---

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [April 22, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/called-javascript-method-and-ruby-method-with-1-button/92645/2 "2007-04-22T17:31:19Z")

</div>

You can use the link\_to\_function helper to setup the call to JS or  
just use straight js to do that. Inside your js function use  
Ajax.Request or one of its subclasses to call the controller action  
using XmlHttpRequest.

Michael

On Apr 22, 3:06 am, Lalala L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [April 28, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/called-javascript-method-and-ruby-method-with-1-button/92645/3 "2007-04-28T13:26:10Z")

</div>

You can use button\_to\_function with remote\_function to create a button  
that will use Ajax to call an action in your controller.

The method remote\_function takes the same parameters as  
link\_to\_remote, which means that you can then use parameters  
like :before, :after, :complete, etc. to call arbitrary JavaScript at  
various points during the Ajax request.

For example:

\<%= button\_to\_function(“Click me”, remote\_function(:url =\> {:action  
=\> :do\_stuff}, :before =\> “myJavaScriptFunction()”)) %\>

Chris

On Apr 22, 11:06 am, Lalala L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
