# How to conditionally replace an element using RJS template

**URL:** <https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265>\
**Category:** Rails\
**Created:** [July 1, 2007, 9:22am UTC](https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265 "2007-07-01T09:22:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mikel](https://www.ruby-forum.com/u/mikel)\
**Post date:** [July 1, 2007, 9:22am UTC](https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265/1 "2007-07-01T09:22:53Z")

</div>

I have an RJS template that looks like this:

page.replace “id\_#{@foo.id}”, :partial =\> “row”

But I only want it to execute if “id\_#{@foo.id}” is an element on the  
current page?

something like:

if page.exists?(“id\_#{@foo.id}”)  
page.replace “id\_#{@foo.id}”, :partial =\> “row”  
end

I have read through every API page that I can find and can’t find such  
a beast? What would be the best way to do this?

Thanks

Mikel

---

<div class="post-metadata">

**Author:** ![mikel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mikel](https://www.ruby-forum.com/u/mikel)\
**Post date:** [July 1, 2007, 11:10am UTC](https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265/2 "2007-07-01T11:10:00Z")

</div>

not sure, but try this:

page \<\< “if $(‘id\_#{@foo.id}’) {”  
page.replace “id\_#{@foo.id}”, :partial =\> “row”  
page \<\< “}”

you can feed straight Javascripts into the page object with \<\< (as  
documented in the API), so i think it _should_ work

---

<div class="post-metadata">

**Author:** ![mikel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mikel](https://www.ruby-forum.com/u/mikel)\
**Post date:** [July 1, 2007, 11:43am UTC](https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265/3 "2007-07-01T11:43:03Z")

</div>

Thanks for the pointer!

You were almost there, the solution was:

page \<\< “elem = $(‘id\_(#{@foo}’);”  
page \<\< “if (elem) {”  
page.replace “id\_(#{@foo}”, :partial =\> “row”  
page \<\< “}”

Needed the extra step.

Thanks 🙂

Mikel

---

<div class="post-metadata">

**Author:** ![mikel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mikel](https://www.ruby-forum.com/u/mikel)\
**Post date:** [July 1, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-conditionally-replace-an-element-using-rjs-template/108265/4 "2007-07-01T13:31:14Z")

</div>

On 7/1/07, Mikel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Needed the extra step.
> 
> Thanks 🙂
> 
> Mikel

You shouldn’t need an extra step. Just some brackets.

page \<\< “if( $(‘id\_#{@foo.id [id\_#removed\_email\_address@domain.invalid](mailto:id_#removed_email_address@domain.invalid)}’) ){”  
page.replace “id\_#{@foo.id [id\_#removed\_email\_address@domain.invalid](mailto:id_#removed_email_address@domain.invalid)}”, :partial =\> “row”  
page \<\< “}”

That should be the equivalent of what you had.

Daniel
