# Pass Ruby Array to Javasript Problem

**URL:** <https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182>\
**Category:** Rails\
**Created:** [September 9, 2009, 11:26am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182 "2009-09-09T11:26:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 11:26am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/1 "2009-09-09T11:26:42Z")

</div>

I have an array named weight\_array. Now i want to pass it to my  
javascript function abc… How can i pass it … May be a stupid question  
but did’nt find it by googling also …

Currently i am doing like this.  
\<%= radio\_button “ab”, “cd”, { :onclick =\> “abc(weight\_array)”; } %\>

Tried this also …  
\<%= radio\_button “ab”, “cd”, { :onclick =\> "\<%= abc(weight\_array) %\> ";  
} %\>

Tried this as well …  
\<%= radio\_button “ab”, “cd”, { :onclick =\> “abc(weight\_array.to\_json)”;  
} %\>

Also after resolving it, i want to know that if i need to pass the ruby  
array with join function …then how can we pass …  
I mean the function [weight\_array.join(", ") …] …

Need to know each and every possible answer …

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 11:37am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/2 "2009-09-09T11:37:46Z")

</div>

Please clarify, abc is the javascript function and weight\_array is the  
ruby  
array, right?

## Thanks, Abhinav

à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

On Wed, Sep 9, 2009 at 2:56 PM, Hemant B. \<

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 11:39am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/3 "2009-09-09T11:39:04Z")

</div>

Yes Exactly …

Abhinav S. wrote:

> Please clarify, abc is the javascript function and weight\_array is the  
> ruby  
> array, right?
> 
> ## Thanks, Abhinav
> 
> à¤…à¤­à¤¿à¤¨à¤µ  
> [http://twitter.com/abhinav](http://twitter.com/abhinav)
> 
> On Wed, Sep 9, 2009 at 2:56 PM, Hemant B. \<

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 11:45am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/4 "2009-09-09T11:45:58Z")

</div>

Although I know very little javasacript, but try this as well:

\<%= radio\_button “ab”, “cd”, { :onclick =\> “abc( " + \<%= weight\_array  
%\> +  
" )”; } %\>

BTW, you should use join to make weight\_array a string before passing to  
javascript function. You can recreate the array in javascript, if you  
wish,  
by splitting back.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

On Wed, Sep 9, 2009 at 3:09 PM, Hemant B. \<

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/5 "2009-09-09T12:08:13Z")

</div>

Did you try converting weight\_array into a string first? Because  
javascript does not understand ruby objects.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

On Wed, Sep 9, 2009 at 3:20 PM, Hemant

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/6 "2009-09-09T13:31:22Z")

</div>

Use JSON for passing array data between Ruby and Javascript. I think  
you missed the #{} inside the string above.

\<%= radio\_button “ab”, “cd”, { :onclick =\> “abc(#  
{weight\_array.to\_json})”; } %\>

On Sep 9, 2:26 pm, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/7 "2009-09-09T14:04:47Z")

</div>

Thanks resolved …  
Resolved using javascript … Manipulating that array in javascript only  
… 🙂

Mukund wrote:

> Use JSON for passing array data between Ruby and Javascript. I think  
> you missed the #{} inside the string above.
> 
> \<%= radio\_button “ab”, “cd”, { :onclick =\> “abc(#  
> {weight\_array.to\_json})”; } %\>
> 
> On Sep 9, 2:26ï¿½pm, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [September 9, 2009, 11:50am UTC](https://www.ruby-forum.com/t/pass-ruby-array-to-javasript-problem/175182/8 "2009-09-09T11:50:32Z")

</div>

Tried but of no worth …  
Newayz thanks a lot for replying … 🙂

Cheers …

Abhinav S. wrote:

> Although I know very little javasacript, but try this as well:
> 
> \<%= radio\_button “ab”, “cd”, { :onclick =\> “abc( " + \<%= weight\_array  
> %\> +  
> " )”; } %\>
> 
> BTW, you should use join to make weight\_array a string before passing to  
> javascript function. You can recreate the array in javascript, if you  
> wish,  
> by splitting back.

> On Wed, Sep 9, 2009 at 3:09 PM, Hemant B. \<
