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
appreciated.
Thanks in advance.

Hi Ichiro,

On Fri, 2010-01-15 at 19:38 +0100, Ichiro S. wrote:

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
appreciated.
Thanks in advance.

If the local variable is a simple object like a number or a string, just
pass its value, not the object itself, using the .to_s method. If it’s
a more complex object, you’ll want to convert it to a json object and
pass that.

HTH,
Bill

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:

If the local variable is a simple object like a number or a string, just
pass its value, not the object itself, using the .to_s method. If it’s
a more complex object, you’ll want to convert it to a json object and
pass that.

HTH,
Bill

On Jan 16, 6:13Â am, Ichiro S. [email protected] wrote:

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?

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 %>)

or

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

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

LeFnod

Ritchie Young wrote:

On Jan 16, 6:13Â am, Ichiro S. [email protected] wrote:

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?

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 %>)

or

<%= “js_function(#{local_variable.to_s})” %>
It works like a charm, thank you Richie. Also thanks to Lefnod and
Bill.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs