Minh
Minh

Reputation: 29

Is it possible to pass Rails variable into JavaScript function?

I have this code:

<% @hello = "hello world" %>
<td class="text"><%= link_to project.name, project,
                  :onmouseout=>"hidetooltip()",
                  :onmouseover=>"showDescription()"%>
</td>

The showDescription JavaScript function takes a string parameter. I want to pass @hello as a parameter to showDescription. I tried everything and can't seem to get it to work.

Upvotes: 0

Views: 405

Answers (2)

MQuy
MQuy

Reputation: 65

another way to do that is using gem client variable. It helps you to do it easily

Upvotes: 0

apneadiving
apneadiving

Reputation: 115531

Here you go:

<% @hello = "hello world" %>
<%= link_to project.name, project, :onmouseout=>"hidetooltip()",
                                   :onmouseover=>"showDescription(#{ @hello.to_json })"%>

Upvotes: 2

Related Questions