passing data to javascript function

Hi, everyone. Is it possible to pass a local variable to a javascript
function? I have a javascript function in application.js, and try to
pass a local variable to the function in index.html.erb. If I passed a
string like 'the string', it worked fine. But when I passed a local
variable, the function didn't run. The function works fine except the
fact that it can't recognize the local variable. Any hint would be
Thanks in advance.

Hi Ichiro,

Thanks for the reply, Bill.
The local variable is an element in an array of strings. When I tried
js_function(local_variable.to_s), it said local_variable is undefined.
When I tried js_function(local_variable), the output was "object
HTMLDivElement". Did I miss someting?

bill walton wrote:

give the value of such function wat you want, but as string,
such as this in a view:
  :onclick => "toggle_memos('#{memos[:name]}')"
  :onclick => "toggle_memos('something')"
and in your *.js
  function toggle_memos(foo) {
    // doSomethingUsefullWithFoo


You would need some escaping so that the "local_variable.to_s" bit
gets evaluated by Ruby. Like maybe:

js_function(<%= local_variable.to_s %>)


<%= "js_function(#{local_variable.to_s})" %>

Ritchie Young wrote: