# RJS help

**URL:** <https://www.ruby-forum.com/t/rjs-help/75368>\
**Category:** Rails\
**Created:** [November 9, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/rjs-help/75368 "2006-11-09T13:46:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 9, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/rjs-help/75368/1 "2006-11-09T13:46:33Z")

</div>

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](http://blog.publishedperspectives.com)

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 4:41am UTC](https://www.ruby-forum.com/t/rjs-help/75368/2 "2006-11-10T04:41:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 9, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/rjs-help/75368/3 "2006-11-09T22:38:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 9:03am UTC](https://www.ruby-forum.com/t/rjs-help/75368/4 "2006-11-10T09:03:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/rjs-help/75368/5 "2006-11-10T12:58:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/rjs-help/75368/6 "2006-11-10T14:04:10Z")

</div>

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

On 11/10/06, Bharat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Bharat
> 
> >

–

* * *

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 6:27am UTC](https://www.ruby-forum.com/t/rjs-help/75368/7 "2006-11-10T06:27:19Z")

</div>

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

On 11/9/06, Bharat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hope this makes the scenario clearer.
> 
> Regards,  
> Bharat
> 
> >

–

* * *

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

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 10, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/rjs-help/75368/8 "2006-11-10T16:20:38Z")

</div>

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);  
}  
}

---

<div class="post-metadata">

**Author:** ![Bharat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Bharat](https://www.ruby-forum.com/u/Bharat)\
**Post date:** [November 11, 2006, 8:42am UTC](https://www.ruby-forum.com/t/rjs-help/75368/9 "2006-11-11T08:42:45Z")

</div>

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
