# Change a value inside button\_to\_function with js

**URL:** <https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485>\
**Category:** Rails\
**Created:** [April 9, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485 "2007-04-09T18:57:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 9, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/1 "2007-04-09T18:57:50Z")

</div>

Hi All

I’m using the following in my .rhtml

\<%= button\_to\_function ‘setup’, remote\_function( :url =\> { :action =\>  
‘abc’, :controller =\> ‘def’, :xyz =\> ‘10’}) %\>"

However, depending on what the user does, I would like to change the  
value for ‘xyz’ using javascript (the server will do stuff depending on  
this value)  
So, this value is known inside the javascript object I’m using, but how  
do I get it dynamically inside the button\_to\_function ?

Thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 9, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/2 "2007-04-09T20:57:18Z")

</div>

Maybe I should ask the question differently!

Can I include javascript stuff inside the button\_to\_function helper ?  
or  
can I use something like \<%= button\_to\_function … %\> inside  
javascript (.js file) ?

I know one way to solve this, which is to look at the source of the  
created html, copy everything thats created by the ‘button\_to\_function’  
helper and put this in my .js files and use the innerHTML to replace the  
button each time the values change. But that doesn’t sound like the  
correct way of working with RoR.

Any suggestions ?

Thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 10, 2007, 12:53am UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/3 "2007-04-10T00:53:06Z")

</div>

Hi,

I’ve been toying with something similar which might help you. In  
remote\_function method, I wanted to insert some javascript to  
dynamically calculate the URL but the remote\_function method wanted to  
use a string for URL. So I kludged my way through it with the  
following:

remote\_function(:url =\> “‘+this.form.action+’”, :update =\> “‘+this.id  
+’\_update”, …[other params]…)

Can you see what I’m doing? It’s hard to read - note that everything  
between the double quotes is all javascript. It’s easier to read once  
you know that. Basically, I’m writing javascript that closes the  
string quote that remote\_function opens for :url, and then I insert  
some javascript code (and reopen the string and add text in the case  
of the “update” param). This results in Javascript that looks like:

new Ajax.Updater(‘’+this.id+‘\_update’, ‘’+this.form.action+‘’)

I think it’s a pretty clean way of generating dynamic JS code in Rails  
js helper functions. It’s a little fugly but not too bad…

hth,

Steve

On Apr 9, 3:10 pm, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 10, 2007, 12:10am UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/4 "2007-04-10T00:10:10Z")

</div>

give the button an id, then you should be able to access the element and  
change any value through the DOM. the button\_to helper will allow you to  
do this, but im not sure if button\_to\_function will - you may have do  
some html yourself.

eg

LuCa wrote:

> Maybe I should ask the question differently!
> 
> Can I include javascript stuff inside the button\_to\_function helper ?  
> or  
> can I use something like \<%= button\_to\_function … %\> inside  
> javascript (.js file) ?
> 
> I know one way to solve this, which is to look at the source of the  
> created html, copy everything thats created by the ‘button\_to\_function’  
> helper and put this in my .js files and use the innerHTML to replace the  
> button each time the values change. But that doesn’t sound like the  
> correct way of working with RoR.
> 
> Any suggestions ?
> 
> Thnx  
> LuCa

---

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 11, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/5 "2007-04-11T19:04:35Z")

</div>

so finally time to try your suggestions, what I see is (in the generated  
HTML code):

\<input id=“conf\_button” onclick="new  
Ajax.Request(’/my\_controlller/my\_action?game=%27%2Bmy\_js\_object…

So you see that I get thing like %27%sB for the '+

All this is generated in the .rhtml file, maybe you are doing this in a  
.js file ?

Hopy you have an idea of what I do wrong!

Thnx  
LuCa

---

<div class="post-metadata">

**Author:** ![luca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/51bf81/32.png) [@luca](https://www.ruby-forum.com/u/luca)\
**Post date:** [April 23, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/change-a-value-inside-button-to-function-with-js/91485/6 "2007-04-23T19:48:35Z")

</div>

Hi,

I don’t think you’re doing anything wrong. The conversion to %27 is  
normal behavior - the javascript is being “escaped” by Ruby. That’s  
the benefit of calling the ruby functions for javascript. You could do  
the same thing with .rjs files - I prefer to write everything in  
“normal” Rails.

Let me know if you’re still having trouble (you can reply to me  
directly).

Steve
