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! -- Posted via http://www.ruby-forum.com/.
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.
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! -- Posted via http://www.ruby-forum.com/.
katya 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.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.
> 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 ) %> >-- Posted via http://www.ruby-forum.com/.