# Ruby variable and java script array?

**URL:** <https://www.ruby-forum.com/t/ruby-variable-and-java-script-array/90609>\
**Category:** Rails\
**Created:** [March 30, 2007, 8:27am UTC](https://www.ruby-forum.com/t/ruby-variable-and-java-script-array/90609 "2007-03-30T08:27:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [March 30, 2007, 8:27am UTC](https://www.ruby-forum.com/t/ruby-variable-and-java-script-array/90609/1 "2007-03-30T08:27:37Z")

</div>

Hi everyone,  
I am in a situation where I have a ruby var x and I like to pass that  
var x to javascript array at index x  
Is there a way of doing such a thing?

---

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [March 30, 2007, 11:24am UTC](https://www.ruby-forum.com/t/ruby-variable-and-java-script-array/90609/2 "2007-03-30T11:24:44Z")

</div>

Lalala L. wrote:

> Hi everyone,  
> I am in a situation where I have a ruby var x and I like to pass that  
> var x to javascript array at index x  
> Is there a way of doing such a thing?

Is this supposed to happen when the page loads or during some sort of  
browser event when the user is viewing the page? If it’s on page load  
you can just write some ERb in the .rhtml file to pass the value of the  
ruby var into the JavaScript array. If it’s on a browser event and you  
want the value to be set without a page refresh you can use Ajax.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![lalala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb8c5e/32.png) [@lalala](https://www.ruby-forum.com/u/lalala)\
**Post date:** [March 31, 2007, 8:30am UTC](https://www.ruby-forum.com/t/ruby-variable-and-java-script-array/90609/3 "2007-03-31T08:30:43Z")

</div>

Michael W. wrote:

> Lalala L. wrote:
> 
> > Hi everyone,  
> > I am in a situation where I have a ruby var x and I like to pass that  
> > var x to javascript array at index x  
> > Is there a way of doing such a thing?
> 
> Is this supposed to happen when the page loads or during some sort of  
> browser event when the user is viewing the page? If it’s on page load  
> you can just write some ERb in the .rhtml file to pass the value of the  
> ruby var into the JavaScript array. If it’s on a browser event and you  
> want the value to be set without a page refresh you can use Ajax.
> 
> –  
> Michael W.

thank you
