# RJS to find an element

**URL:** <https://www.ruby-forum.com/t/rjs-to-find-an-element/100715>\
**Category:** Rails\
**Created:** [July 31, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715 "2006-07-31T17:50:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [July 31, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715/1 "2006-07-31T17:50:44Z")

</div>

I’m trying to implement a remove from cart method based on the  
examples in the second edition Agile book. I have everything working  
except for the RJS that removes items from the cart. Ideally what  
I’d like to do is highlight an element that I am deleting only if  
there is more than one of that particular item in the cart. If the  
item deleted is the only of it’s kind it gets removed from the  
document before the highlight is “activated”.

page[:cart].replace\_html :partial =\> ‘cart’, :object =\> @cart

page[@div].visual\_effect :highlight,  
:startcolor =\> “#88ff88”,  
:endcolor =\> “#114411”

Is there a way to use RJS to see if page[@div] (in this case) exists  
before I try to do the highlight?

Thanks,  
Jake

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [July 31, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715/2 "2006-07-31T17:51:43Z")

</div>

Jake S. wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Check out  
[http://www.akropolix.net/rik0/blogs/2006/06/14/rjs-rails-if-statement/](http://www.akropolix.net/rik0/blogs/2006/06/14/rjs-rails-if-statement/)  
It shows how to select the div if it exists and then run some javascript  
happiness on it.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [July 31, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715/3 "2006-07-31T18:34:03Z")

</div>

Matthew,  
Thanks for your reply, however, I can’t seem to get it to work  
properly. Maybe it is because in certain cases I am removing that  
element from the document before I get to the “search”, or maybe  
there is an error in my implementation.

page[:cart].replace\_html :partial =\> ‘cart’, :object =\> @cart

page.select(@div).each do |page|

page[page].visual\_effect :highlight,  
:startcolor =\> “#88ff88”,  
:endcolor =\> “#114411”  
end

Thanks,  
Jake

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [August 3, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715/4 "2006-08-03T14:24:43Z")

</div>

On Monday, July 31, 2006, at 11:13 AM, Jake S. wrote:

> On Jul 31, 2006, at 10:51 AM, Matthew M. wrote:
> 
> > > Jake
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> I’m beginning to think that RJS templates should provide NO helper  
> functions at all. Just a big text block that gets sent back. That way  
> we would all have to program the response in JS, and we wouldn’t get  
> tempted to use ruby control structures. It can be very confusing for  
> people to grasp the concept that the RJS output is executed in the  
> context of the client browser, rather than on the server side.

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

---

<div class="post-metadata">

**Author:** ![jake\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jake\_s](https://www.ruby-forum.com/u/jake_s)\
**Post date:** [August 1, 2006, 4:39am UTC](https://www.ruby-forum.com/t/rjs-to-find-an-element/100715/5 "2006-08-01T04:39:50Z")

</div>

Here’s what I did to solve my problem, it seems dirty and clunky. If  
anybody has a better solution, I’m open to suggestions. From within  
the model I’m returning an array (to the controller) that has the id  
of the element to update as well as a variable that determines if the  
highlight effect will be displayed by the RJS template.

Cart model:

def remove\_cart\_item(product)  
current\_item = @items.find { |item| item.product == product }  
if current\_item.quantity \> 1  
current\_item.decrement\_quantity #defined in cart\_item model  
@effect = 1  
else  
@items.delete(current\_item)  
@effect = 0  
end  
current\_item = [“product” + current\_item.id.to\_s, @effect]  
current\_item  
end

Store controller:

def remove\_item  
begin  
@product = Product.find(params[:id])  
rescue  
logger.error(“Attempt to access invalid product #{params[:id]}”)  
redirect\_to\_index(“Invalid product ‘#{params[:id]}’”)  
else  
@div = @cart.remove\_cart\_item(@product)  
redirect\_to\_index unless request.xhr?  
end  
end

Remobe\_item.rjs:

page[:cart].replace\_html :partial =\> ‘cart’, :object =\> @cart

if @div[1] == 1  
page[@div[0]].visual\_effect :highlight,  
:startcolor =\> “#88ff88”,  
:endcolor =\> “#114411”  
end

Thanks,  
Jake
