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.

Regards,
Bharat

http://blog.publishedperspectives.com

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
better.

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
seeing.

Please provide an example and perhaps I can assist further.

Chris

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.

Regards,
Bharat

Bharat,

Think about it in a different way. The web page talks
to the server, the server talks back using RJS. The server can
manipulate and update the page using RJS, but by the time you are using
RJS to talk back to the page, your 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,
nathan

Nathan,

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.

Regards,
Bharat

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=' +
escape(document.getElementById('dom_id_surname').value)",
  :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.

Regards,
Andy

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 params[:id]

regards,
nathan

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...

rjs:

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,

Bharat