# Visible elements in RJS

**URL:** <https://www.ruby-forum.com/t/visible-elements-in-rjs/99412>\
**Category:** Rails\
**Created:** [July 22, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412 "2006-07-22T12:27:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 22, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412/1 "2006-07-22T12:27:02Z")

</div>

How can I know if a an element is visible or hidden in RJS

I’m trying to hide/show some data but it does not work  
Error =\> parse error, unexpected $undefined.

#show\_doctor.rjs  
if($("#{@element}").visible())  
page.hide “hide\_#{@element}”  
end

Any ideas?

@element contains the value of the html element

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 22, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412/2 "2006-07-22T19:42:07Z")

</div>

Rath — wrote:

> How can I know if a an element is visible or hidden in RJS
> 
> I’m trying to hide/show some data but it does not work  
> Error =\> parse error, unexpected $undefined.
> 
> #show\_doctor.rjs  
> if($("#{@element}").visible())  
> page.hide “hide\_#{@element}”  
> end
> 
> Any ideas?
> 
> @element contains the value of the html element

This code should work if @element contains the id of the html element,  
what do you have exactly in @element ?  
Btw since hiding an already hidden element won’t do anything you don’t  
even need to test if it is visible ^^  
You can then rewrite it like this:  
page[“hide\_#{@element}”].hide()

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 22, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412/3 "2006-07-22T20:04:14Z")

</div>

On Saturday, July 22, 2006, at 7:42 PM, Julien S. wrote:

> > 
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I seem to recall that you can do something like:

page[‘element’].toggle

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 23, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412/4 "2006-07-23T13:54:50Z")

</div>

Thanks Kevin

Yes, there is a page[‘element’].toggle function but the problem is that  
I need to check if an object is displayed, if not I want to display it  
and at the same time do more stuff… (ie hide or show more elements)

I had to do a quick hack, perhaps it is not the best way to do it but  
for the time been I will do the following

I’m sending the id with the hide\_ or show\_ prefix and then check

if @element.starts\_with?(“show\_”)  
page.show “whatever”  
more stuff  
else  
page.hide “whatever”  
something else  
end

Any ideas are welcome

Cheers

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [July 23, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/visible-elements-in-rjs/99412/5 "2006-07-23T16:11:06Z")

</div>

On Sunday, July 23, 2006, at 1:54 PM, Rath — wrote:

> Cheers
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

If it works, that’s great.  
The problem with RJS in this case is that the RJS template needs to be  
rendered in one shot before sending it to the browser. There is no way  
to query JS objects on the other side and have that alter the rendering.

I see two ways to deal with this.

One would be to use the RJS template to generate a JS script that could  
do the conditional processing on the client browser, and the other way  
would be to pack all the information you need into a proxy object that  
gets sent along with the request (sort of like how you have already done  
it).

You could do something like this…

page \<\< “if $(‘obj’).visible {do\_something()} else  
{do\_something\_else()}”

You could predefine the two functions into a JS library, or you could  
generate them and send them back with the RJS request.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
