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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Ichiro, On Fri, 2010-01-15 at 19:38 +0100, Ichiro Saga 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On 15 Jan., 23:13, Ichiro Saga <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 16, 6:13 am, Ichiro Saga <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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})" %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ritchie Young wrote:> On Jan 16, 6:13 am, Ichiro Saga <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. -- Posted via http://www.ruby-forum.com/. --0015174c0d5c8c83d2047d74534b Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0015174c0d5c8c83d2047d74534b--