# Verifying existence of elements

**URL:** <https://www.ruby-forum.com/t/verifying-existence-of-elements/113488>\
**Category:** Rails\
**Created:** [August 31, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/verifying-existence-of-elements/113488 "2007-08-31T16:51:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 31, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/verifying-existence-of-elements/113488/1 "2007-08-31T16:51:01Z")

</div>

My controller ends like this:

```
render :update do |page|
  page.replace_html 'someDiv', :partial => 'somePartial'
end

```

I need to edit this to verify that someDiv exists before I go  
replacing\_htmling it. I’m at a loss to figure out if this can be  
done. Any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 31, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/verifying-existence-of-elements/113488/2 "2007-08-31T18:33:13Z")

</div>

On 8/31/07, Josh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My controller ends like this:
> 
> ```
> render :update do |page|
> page.replace_html 'someDiv', :partial => 'somePartial'
> end
> 
> ```
> 
> I need to edit this to verify that someDiv exists before I go  
> replacing\_htmling it. I’m at a loss to figure out if this can be  
> done. Any ideas?

are you not in control of the content of your pages? As far as I  
know, the only way to do something like this would be to read the  
entire page and search to make sure the div exists before continuing.  
Unfortunately you can’t just execute the rjs call and expect it to  
throw an exception on error (the exception is thrown outside the  
context of rails and handled directly by the prototype library)

You can use the following library to allow you to parse your html files:

[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)

but of course it’ll be faster when operating on local files than  
through using open-uri. But this is an expensive operation which you  
really want to avoid if possible.

Adam

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [September 1, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/verifying-existence-of-elements/113488/3 "2007-09-01T16:42:16Z")

</div>

Parse the entire page with hpricot? That’s rather silly. You can use  
basic javascript or the prototype library to check for the existance  
of an element with an id, and you can use use the concatenation  
operator to add plain javascript to the output:

page \<\< ‘if (${‘someDiv’)) {’  
page.replace\_html ‘someDiv’, :partial =\> ‘somePartial’  
page \<\< ‘}’

It’s not the most elegant solution because you’re not writing pure  
ruby any more, but it’s quick and simple if you know a little  
javascript.

Rein

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [September 1, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/verifying-existence-of-elements/113488/4 "2007-09-01T19:20:33Z")

</div>

Whoops, typo:

page \<\< ‘if (${‘someDiv’)) {’

should be:

page \<\< ‘if ($(‘someDiv’)) {’
