RJS help

I need to get the innerHTML of a div in an rjs template. How do I go
about doing this?
I have already tried accessing it using page[‘div1’].innerHTML but
rails complains that innerHTML is not a function.
I tried writing a javascript function, but there does not seem to be a
way of getting the return value when the page.call is used to call
javascript functions.

Any help will be appreciated.



Hi Chris,

Basically I am doing an ajax update of a div. I would like to ensure
that the new text coming down from the server is not already present in
the div. I understand that RJS is executed on the server and
translated to javascript. I was hoping that there is a RJS method which
translates into getting an innerHTML attribute.

Hope this makes the scenario clearer.


I’m not sure I understand you 100% so forgive me if I misunderstood
but it sounds like you want to get the contents of an element (the
innerHTML) via code on the server. You can’t do that. If I could see
an example of what it is you are trying to do, I might understand

Keep in mind that the rjs code gets ‘translated’ into javascript which
is then sent back to the browser and then evaluated.

page[‘div1’].innerhtml is seen by rails as ruby code, that’s why you
are getting the error. there is no innerHTML method, as you are

Please provide an example and perhaps I can assist further.



I did think of trying that however the problem is I am unable to get
information from js into ruby code. I could for ex pass the innerhtml
as a string as part of a link_to_remote call but how do I get the
innerhtml into a ruby variable.


I have had a similar problem before and have dropped down into
Javascript to send of the value of a dom element, in my case sending
multiple values of a form in an observe function. To which i used this.

<%= observe_field :postcode,
:url => {:action => “dupelist”},
:frequency => 0.5,
:with => “‘surname=’ +
:update => “dupelist”
and within the controller access surname via params[:surname].

i know that this is not a link_to_remote function but at least an idea
you can work from.


This is untested, but something like this might work for you:

<%= link_to_function ‘click here’, “getMyDiv()” %>

then in your controller you can access whatever is inside the div with


On 11/10/06, Bharat [email protected] wrote:


portland, or: 01 (503) 608-7950
cuernavaca, mx: 52 (777) 318-9094


Think about it in a different way. The web page talks to the server,
server talks back using RJS. The server can manipulate and update the
using RJS, but by the time you are using RJS to talk back to the page,
round trip is over.

Depending on what you are trying to do, you might use a bit of
javascript to
read the innerHtml of a div, send it to the server, then the server will
know what is inside that div and can respond back to the page using RJS.

hope this helps,

On 11/9/06, Bharat [email protected] wrote:

Hope this makes the scenario clearer.


portland, or: 01 (503) 608-7950
cuernavaca, mx: 52 (777) 318-9094

if you don’t need to do anything on the server side with the element
content, ie, you’re only using it for comparison purposes, then don’t
send it. do the comparison on the browser side. you’ve already sent
an ajax request at this point, might as well send something back with
the response.

off the top of my head…


page.call(‘myfunction’, ‘div1’, ‘newcontent’)

js function:

function myfunction(element, content) {
// only update if content is different
if($(element).innerHTML != content) {
Element.update(element, content);

Thanks a lot folks!! Problem solved, I use Chris’s method,

however it would have been nice if I could have done the code
generation using rjs rather than write javascript. oh well! maybe this
will get into rjs sometime later.

Thanks again,


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs