# Evaluate ruby code inside javascript

**URL:** <https://www.ruby-forum.com/t/evaluate-ruby-code-inside-javascript/139262>\
**Category:** Rails\
**Created:** [June 2, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/evaluate-ruby-code-inside-javascript/139262 "2008-06-02T12:29:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 2, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/evaluate-ruby-code-inside-javascript/139262/1 "2008-06-02T12:29:08Z")

</div>

Hi  
I was trying to call a javascript function.I have text areas like  
transfer\_reason1  
transfer\_reason2 etc in each divs  
div number is available as variable div\_no in the page…How can I pass  
the textarea content to the javascript…Below I tried like  
get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason+eval(#{div\_no}))’).value  
)

How can I evaluate the above to get  
get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason1’).value )  
get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason2’).value ) etc  
But not working.Please help

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 2, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/evaluate-ruby-code-inside-javascript/139262/2 "2008-06-02T12:37:33Z")

</div>

Sijo Kg wrote:

> Hi  
> I was trying to call a javascript function.I have text areas like  
> transfer\_reason1  
> transfer\_reason2 etc in each divs  
> div number is available as variable div\_no in the page…How can I pass  
> the textarea content to the javascript…Below I tried like  
> get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason+eval(#{div\_no}))’).value  
> )
> 
> How can I evaluate the above to get  
> get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason1’).value )  
> get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason2’).value ) etc  
> But not working.Please help
> 
> Sijo

You’re mixing javascript with ruby as far as I can see.  
You can’t ‘call’ js functions as long as your code runs on the server  
(ruby code)  
The js stuff (vars etc) is only available in the browser. Same goes for  
dom object methods like value.  
So div\_no won’t be available to ruby.

get\_primary\_assignees\_transfer\_reason($(‘transfer\_reason’ +  
div\_no))’).value)

should work in the browser. but will never work on the server.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 2, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/evaluate-ruby-code-inside-javascript/139262/3 "2008-06-02T12:42:31Z")

</div>

On 2 Jun 2008, at 11:29, Sijo Kg wrote:

> 

assuming div\_no is a javascript variable, that should be $  
(‘transfer\_reason’ + div\_no)

Fred
