Change a value inside button_to_function with js

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

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

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. [email protected]

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

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

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