# RJS - checking if element exists?

**URL:** <https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238>\
**Category:** Rails\
**Created:** [November 8, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238 "2006-11-08T12:27:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/1 "2006-11-08T12:27:47Z")

</div>

Hiya,

I’m trying to get around the problem of not knowing whether to use  
insert\_html or replace\_html as there will be times when I will need to  
add new elements or replace existing ones. Is there some form of check  
to see if a DOM element exists on the page? Or can I catch errors so  
that I know there isn’t an element of a certain name already there?

Hope that makes sense,  
Bex

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/2 "2006-11-08T13:21:46Z")

</div>

You (obviously) can’t do this purely server side, you can however as  
part of your rjs generate the necessary javascript to test for this.  
So, in fugly land you can do this

page\<\< “if($(‘element\_id’)){”  
page[‘element\_id’].hide]’  
page \<\< “}”

However this is a plugin  
[http://www.agilewebdevelopment.com/plugins/rjs\_if\_unless\_blocks](http://www.agilewebdevelopment.com/plugins/rjs_if_unless_blocks) that  
allows you to instead do

page.if page[element\_id].visible do  
page[element\_id].hide  
end

Fred

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/3 "2006-11-08T13:44:57Z")

</div>

On 8 November 2006 14:27, Becky F. wrote:

> I’m trying to get around the problem of not knowing whether to use  
> insert\_html or replace\_html as there will be times when I will need to  
> add new elements or replace existing ones. Is there some form of check  
> to see if a DOM element exists on the page? Or can I catch errors so  
> that I know there isn’t an element of a certain name already there?  
> There are at least two solutions:

1. Create custom javascript methods, e.g.  
function create\_or\_update\_element(id, content) {  
…  
}

2. Install js-if-blocks plugin  
./script/plugin install  
svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks

and then write your RJS code like this:

page.if page[my\_element\_id] do  
page.replace\_html element\_id, content  
page.else  
page.insert\_html :top, parent\_id,  
“

#{content}
”  
end

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/4 "2006-11-08T14:37:06Z")

</div>

Thanks so much for the replies. Am I right in thinking that these  
plugins will work if the element itself isn’t there? I tried:

page.if page[‘test’] do  
puts “Working!”  
page.else  
puts “Else working!”  
end

Where test does not exist on the page, and I get an error saying If is  
not defined :os

Thanks again for the help!  
Bex

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/5 "2006-11-08T16:17:17Z")

</div>

> > Where test does not exist on the page, and I get an error saying If is  
> > not defined :os  
> > As far as I understand, you haven’t installed plugin yet. The error is  
> > because “page.if” method is defined in that plugin and not part of  
> > standard  
> > Rails distribution.

Ah I see. I’m working on windows and I wasn’t sure if the plugin had  
installed or not, as I have no error message when i (from the root of my  
app) type: ruby script/plugin install  
svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks

I can just see it being something stupid I’m doing wrong, but I’m  
assuming that doesn’t install the plugin?

Thanks again!  
Bex

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/6 "2006-11-08T16:01:27Z")

</div>

On 8 November 2006 16:37, Becky F. wrote:

> Thanks so much for the replies. Am I right in thinking that these  
> plugins will work if the element itself isn’t there?

1. Actually, those two references are to the same plugin
2. Yes, the condition will work.

> I tried:
> 
> page.if page[‘test’] do  
> puts “Working!”  
> page.else  
> puts “Else working!”  
> end

“puts” will not work, because, actually, there is no true “if”  
statement.  
Those are just methods that generate proper javascript. So, in your  
case, you  
should use something like page.alert(‘Working!’)

> Where test does not exist on the page, and I get an error saying If is  
> not defined :os  
> As far as I understand, you haven’t installed plugin yet. The error is  
> because “page.if” method is defined in that plugin and not part of  
> standard  
> Rails distribution.

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/7 "2006-11-08T17:05:42Z")

</div>

On 8 November 2006 18:17, Becky F. wrote:

> I can just see it being something stupid I’m doing wrong, but I’m  
> assuming that doesn’t install the plugin?  
> You can check if you have non-empty js-if-blocks directory  
> under ./vendor/plugins directory.

Also, make sure you have restarted you server.

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [November 8, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/rjs-checking-if-element-exists/75238/8 "2006-11-08T19:18:59Z")

</div>

Maxim K. wrote:

> On 8 November 2006 18:17, Becky F. wrote:
> 
> > I can just see it being something stupid I’m doing wrong, but I’m  
> > assuming that doesn’t install the plugin?  
> > You can check if you have non-empty js-if-blocks directory  
> > under ./vendor/plugins directory.
> 
> Also, make sure you have restarted you server.

Honestly you have completely solved a massive problem with this plugin!  
Turned out I had the rails versioning wrong or something so once I fixed  
that the plugin installed and worked like a dream! Now back to  
officially loing RJS 😃

Thanks sooooo much!  
Bex
