# passing data to javascript function

**URL:** <https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611>\
**Category:** rubyonrails-talk\
**Created:** [January 15, 2010, 6:38pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611 "2010-01-15T18:38:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 15, 2010, 6:38pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/1 "2010-01-15T18:38:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bill\_Walton1](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_Walton1](https://discuss.rubyonrails.org/u/Bill_Walton1)\
**Post date:** [January 15, 2010, 8:03pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/2 "2010-01-15T20:03:59Z")

</div>

Hi Ichiro,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 15, 2010, 10:13pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/3 "2010-01-15T22:13:34Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![le\_fnord](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@le\_fnord](https://discuss.rubyonrails.org/u/le_fnord)\
**Post date:** [January 15, 2010, 10:46pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/4 "2010-01-15T22:46:55Z")

</div>

give the value of such function wat you want, but as string, such as this in a view: &nbsp;&nbsp;:onclick =\> "toggle\_memos('#{memos[:name]}')" or &nbsp;&nbsp;:onclick =\> "toggle\_memos('something')" and in your \*.js &nbsp;&nbsp;function toggle\_memos(foo) { &nbsp;&nbsp;&nbsp;&nbsp;// doSomethingUsefullWithFoo &nbsp;&nbsp;&nbsp;&nbsp;console.log(foo); &nbsp;&nbsp;};

LeFnod

---

<div class="post-metadata">

**Author:** ![Ritchie](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Ritchie](https://discuss.rubyonrails.org/u/Ritchie)\
**Post date:** [January 16, 2010, 4:01am UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/5 "2010-01-16T04:01:24Z")

</div>

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})" %\>

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 18, 2010, 6:14pm UTC](https://discuss.rubyonrails.org/t/passing-data-to-javascript-function/48611/6 "2010-01-18T18:14:37Z")

</div>

Ritchie Young wrote:
