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

**URL:** <https://www.ruby-forum.com/t/how-to-pass-a-ruby-array-to-a-javascript-function/147016>\
**Category:** Rails\
**Created:** [August 27, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-ruby-array-to-a-javascript-function/147016 "2008-08-27T16:19:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 27, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-ruby-array-to-a-javascript-function/147016/1 "2008-08-27T16:19:41Z")

</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) {  
for (var i = 0; i\<42; i++) {  
cell = this.calendar\_day\_grid[i];  
console.log(calendar\_matrix(i));  
if (calendar\_matrix(i) == 1 ) {cell.addClassName(“closed”);}  
}  
},

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:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 27, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-ruby-array-to-a-javascript-function/147016/2 "2008-08-27T16:44:53Z")

</div>

On 27 Aug 2008, at 15:18, Erwin wrote:

> for (var i = 0; i\<42; i++) {  
> cell = this.calendar\_day\_grid[i];  
> console.log(calendar\_matrix(i));  
> if (calendar\_matrix(i) == 1 ) {cell.addClassName(“closed”);}  
> }  
> },
> 
> 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:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 28, 2008, 10:50am UTC](https://www.ruby-forum.com/t/how-to-pass-a-ruby-array-to-a-javascript-function/147016/3 "2008-08-28T10:50:39Z")

</div>

thanks a lot I realize it

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

On 27 août, 16:44, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
