Forum: Ruby on Rails Passing parameters to Javascript

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
katya (Guest)
on 2005-12-12 20:32
Hello,

I'm trying to call a Javascript function from a Ruby template, and have
zero success passing in parameters.

function myFunction(stringToDisplay) {
  var my_div = document.getElementById("my_div");
  var text = document.createTextNode("String to display is " +
stringToDisplay);
  my_div.appendChild(text);
}

....

<a href="#" onclick="myFunction('<% @hello %>')>Call my javascript
function</a>

If I hardcode parameters value everything works fine. However, I have no
luck passing parameters in. I've tried '<%= @hello -%>', but it only
causes @hello to render.

Please help!
Mark Reginald James (Guest)
on 2005-12-13 00:26
(Received via mailing list)
katya wrote:

> I'm trying to call a Javascript function from a Ruby template, and have
> zero success passing in parameters.
> ....
> <a href="#" onclick="myFunction('<% @hello %>')>Call my javascript
> function</a>
>
> If I hardcode parameters value everything works fine. However, I have no
> luck passing parameters in. I've tried '<%= @hello -%>', but it only
> causes @hello to render.

I'm not clear what you want to do.  What do you want to render if not
the value of @hello?

<a href="#" onclick="myFunction('<%= escape_javascript(@hello)
%>')">Call my javascript function</a>

should work if you want to use the value of @hello at the time
the template is rendered.

--
We develop, watch us RoR, in numbers too big to ignore.
katya (Guest)
on 2005-12-13 20:36
Mark Reginald James wrote:

> I'm not clear what you want to do.  What do you want to render if not
> the value of @hello?

Well, I do want @hello to render, but inside of a specified in the
function div, not where I make a call to the javascipt.
>
> <a href="#" onclick="myFunction('<%= escape_javascript(@hello)
> %>')">Call my javascript function</a>
>
> should work if you want to use the value of @hello at the time
> the template is rendered.

Here is the source of the page once it get rendered after I tried that:

<a href="#" onclick="myFunction('\n\n\n\n\n<table
id=\"mainTabs"....</table>')>Call my javascript
function</a>

Value of @hello is a dynamically constructed html string that I would
like to get rendered in a particular place once the user clicks on a
link.

Thank you for the suggestion!
Mark Reginald James (Guest)
on 2005-12-14 01:46
(Received via mailing list)
katya wrote:

>>the template is rendered.
> link.
I'm still not clear whether it's just a matter of getting the
value of @hello that has been rendered into the parameter of
myFunction to appear on your page, or whether @hello must be
generated at the time the link is clicked.

If the former, and if by the looks of it the string is HTML rather
than text, you should be appending HTML to the div using

<%= link_to_function 'Call my javascript function',
       update_element_function( 'my_div', :position => :bottom, :content
=> @hello ) %>

If the latter, you need an AJAX call. Either

<%= link_to_remote( 'Call my javascript function',
                     :url => {:action => 'get_string'},
                     :success => 'myFunction(request.responseText)' ) %>

or

<%= link_to_remote( 'Call my javascript function',
                     :url => {:action => 'get_string'},
                     :update => 'my_div', :position => :bottom ) %>

CONTROLLER:

def get_string
   @hello = ...
   render :text => @hello
end

--
We develop, watch us RoR, in numbers too big to ignore.
katya (Guest)
on 2005-12-14 20:34
> I'm still not clear whether it's just a matter of getting the
> value of @hello that has been rendered into the parameter of
> myFunction to appear on your page, or whether @hello must be
> generated at the time the link is clicked.

It is the former, and it works great! Thank you so much for your help!

>
> If the former, and if by the looks of it the string is HTML rather
> than text, you should be appending HTML to the div using
>
> <%= link_to_function 'Call my javascript function',
>        update_element_function( 'my_div', :position => :bottom, :content
> => @hello ) %>
>
This topic is locked and can not be replied to.