# Passing a value to a js function via button\_to\_function

**URL:** <https://www.ruby-forum.com/t/passing-a-value-to-a-js-function-via-button-to-function/166423>\
**Category:** Rails\
**Created:** [April 25, 2009, 12:21am UTC](https://www.ruby-forum.com/t/passing-a-value-to-a-js-function-via-button-to-function/166423 "2009-04-25T00:21:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 25, 2009, 12:21am UTC](https://www.ruby-forum.com/t/passing-a-value-to-a-js-function-via-button-to-function/166423/1 "2009-04-25T00:21:31Z")

</div>

I’ve got a table of what I call resources, want to show in a jQuery  
dialog a particular record when user clicks button in a row.

in the table I’m doing:

\<%=button\_to\_function 'Show','showresource1()',:class =\>"ui-button ui-state-default ui-corner-all", :id =\> resource.id %\>

js:

I can open the dialog and load this static URL, but am stumped re  
passing resource.id from the button\_to\_function down to the js function.

Any help would be Much Appreciated!

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 25, 2009, 2:00am UTC](https://www.ruby-forum.com/t/passing-a-value-to-a-js-function-via-button-to-function/166423/2 "2009-04-25T02:00:44Z")

</div>

Bob W. wrote:

> I’ve got a table of what I call resources, want to show in a jQuery  
> dialog a particular record when user clicks button in a row.
> 
> in the table I’m doing:
> 
> \<%=button\_to\_function 'Show','showresource1()',:class =\>"ui-button ui-state-default ui-corner-all", :id =\> resource.id %\>
> 
> js:
> 
> I can open the dialog and load this static URL, but am stumped re  
> passing resource.id from the button\_to\_function down to the js function.
> 
> Any help would be Much Appreciated!

My js is a little rusty, but typically when you want js to execute a  
function after a user clicks on a button, you pass js a reference to the  
function:

yourbutton.onclick = somefunc;

Then when the user clicks on the button, js executes the function using  
the function reference it stored away. But very importantly, js calls  
the function with one argument:

yourbutton.onclick(event)

You can’t change the way js will call the function. js will always call  
the function with one argument–the event object. If you tried this:

yourbutton.onclick = somefunc(arg1, arg2);

then instead of assigning a reference to somefunc to yourbutton.onclick,  
that line would cause somefunc to execute immediately, and then its  
return value would be assigned to yourbutton.onclick.

With a little modification, you can get js to do what you want:

helperfunc wraps another function around your function. If you pass the  
wrapper function to js, then when the user clicks on the button, js will  
execute the wrapper function, which in turn will call your function.  
That allows you to write something like this in js:

yourbutton.onclick =  
helperfunc(“[http://localhost:3000/resources/show/4](http://localhost:3000/resources/show/4)”);

The causes helperfunc() to execute immediately, and helperfunc’s return  
value is what js will call when the user clicks on the function.  
helperfunc’s return value is this:

function () {  
showresoursource(“[http://localhost:3000/resources/show/4](http://localhost:3000/resources/show/4)”); }

After the user clicks on the button, js will call the wrapper function,  
which in turn will call your function with the proper argument.

Because I just started learning rails, I don’t know if rails provides a  
direct solution for that js work around or not. Anyway, that is  
something for you to ponder until someone else posts a rails solution.

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 25, 2009, 2:06am UTC](https://www.ruby-forum.com/t/passing-a-value-to-a-js-function-via-button-to-function/166423/3 "2009-04-25T02:06:25Z")

</div>

7stud – wrote:

> The causes helperfunc() to execute immediately, and helperfunc’s return  
> value is what js will call when the user clicks on the function.

That should be:

That causes helperfunc() to execute immediately, and helperfunc’s return  
value is what gets assigned to yourbutton.onclick. helperfunc’s return  
value is what js will call when the user clicks on the _button_.
