# How to pass a Ruby Array to a javascript function ?

**URL:** <https://discuss.rubyonrails.org/t/how-to-pass-a-ruby-array-to-a-javascript-function/33694>\
**Category:** rubyonrails-talk\
**Created:** [August 27, 2008, 2:18pm UTC](https://discuss.rubyonrails.org/t/how-to-pass-a-ruby-array-to-a-javascript-function/33694 "2008-08-27T14:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwin1](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@Erwin1](https://discuss.rubyonrails.org/u/Erwin1)\
**Post date:** [August 27, 2008, 2:18pm UTC](https://discuss.rubyonrails.org/t/how-to-pass-a-ruby-array-to-a-javascript-function/33694/1 "2008-08-27T14:18:54Z")

</div>

I am passing a ruby array @myArray to a javascript function via rjs

page \<\< "{ $ ('my\_calendar').calendar\_date\_select.classForMatrix('#{@calendar\_matrix}');}"

the js function is the following :

&nbsp;&nbsp;classForMatrix: function(calendar\_matrix) { &nbsp;&nbsp;for (var i = 0; i\<42; i++) { &nbsp;&nbsp;&nbsp;&nbsp;cell = this.calendar\_day\_grid[i]; &nbsp;&nbsp;&nbsp;&nbsp;console.log(calendar\_matrix(i)); &nbsp;&nbsp;&nbsp;&nbsp;if (calendar\_matrix(i) == 1 ) {cell.addClassName("closed");} &nbsp;&nbsp;} &nbsp;&nbsp;},

and I get the following js error TypeError: calendar\_matrix is not a function

where am I wrong ?

thanks for your lights

erwin

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 27, 2008, 2:44pm UTC](https://discuss.rubyonrails.org/t/how-to-pass-a-ruby-array-to-a-javascript-function/33694/2 "2008-08-27T14:44:13Z")

</div>

> I am passing a ruby array @myArray to a javascript function via rjs
> 
> page \<\< "{ $ ('my\_calendar ').calendar\_date\_select.classForMatrix('#{@calendar\_matrix}');}"
> 
> the js function is the following :
> 
> classForMatrix: function(calendar\_matrix) { &nbsp;&nbsp;for (var i = 0; i\<42; i++) { &nbsp;&nbsp;&nbsp;&nbsp;cell = this.calendar\_day\_grid[i]; &nbsp;&nbsp;&nbsp;&nbsp;console.log(calendar\_matrix(i)); &nbsp;&nbsp;&nbsp;&nbsp;if (calendar\_matrix(i) == 1 ) {cell.addClassName("closed");} &nbsp;&nbsp;} },
> 
> and I get the following js error TypeError: calendar\_matrix is not a function
> 
> where am I wrong ?

if calendar\_matrix is an array you need calendar\_matrix[i] not  
calendar\_matrix(i). On top of that you probably need to make it look like an array (ie  
enclose it in , separate values with commas etc...). The  
array\_or\_string\_for\_javascript looks like it will do that for you.

Fred

---

<div class="post-metadata">

**Author:** ![Erwin1](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@Erwin1](https://discuss.rubyonrails.org/u/Erwin1)\
**Post date:** [August 28, 2008, 8:49am UTC](https://discuss.rubyonrails.org/t/how-to-pass-a-ruby-array-to-a-javascript-function/33694/3 "2008-08-28T08:49:56Z")

</div>

thanks a lot I realize it

using the @calendar\_matrix.to\_json send correctly the array and that's it....
