# HTML table visual effect

**URL:** <https://www.ruby-forum.com/t/html-table-visual-effect/177513>\
**Category:** Rails\
**Created:** [October 19, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513 "2009-10-19T15:26:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 19, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513/1 "2009-10-19T15:26:26Z")

</div>

Hello,

I have an HTML table with several rows. I want to change the row color  
when the user clicks it. This is quite simple to do with plain  
javascript, but I want to use rails. Is there any helper or something  
that performs this task for me?

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 19, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513/2 "2009-10-19T16:47:21Z")

</div>

On Mon, Oct 19, 2009 at 6:26 AM, Eduardo B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an HTML table with several rows. I want to change the row color  
> when the user clicks it. This is quite simple to do with plain  
> javascript, but I want to use rails.

You realize this is a client-side interaction, which means Javascript  
no matter how it’s packaged? 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 20, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513/3 "2009-10-20T13:09:49Z")

</div>

Hassan S. wrote:

> On Mon, Oct 19, 2009 at 6:26 AM, Eduardo B.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> You realize this is a client-side interaction, which means Javascript  
> no matter how it’s packaged? 🙂
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

Yes, but like server-side code, javascript must be propper coded, and  
explicit onclick event declaration is wrong.

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [November 4, 2009, 7:03pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513/4 "2009-11-04T19:03:14Z")

</div>

On Oct 19, 5:26 am, Eduardo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have an HTMLtablewith several rows. I want to change therowcolor  
> when the user clicks it. This is quite simple to do with plain  
> javascript, but I want to use rails. Is there any helper or something  
> that performs this task for me?

You might try leveraging the scriptaculous visual effects library for  
this:

[http://wiki.github.com/madrobby/scriptaculous/effects](http://wiki.github.com/madrobby/scriptaculous/effects)

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 20, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/html-table-visual-effect/177513/5 "2009-10-20T14:24:03Z")

</div>

On 20 Oct 2009, at 13:09, Eduardo B. wrote:

> > You realize this is a client-side interaction, which means Javascript  
> > no matter how it’s packaged? 🙂
> 
> Yes, but like server-side code, javascript must be propper coded, and  
> explicit onclick event declaration is wrong.

It’s not wrong, it just looks ugly and can be very verbose if you put  
it on every single row, so a more generic approach is preferrable to  
say the least.

You have a couple of options:

- Either go completely event delegated, mixing in a library  
specifically designed for this like NWEvents can greatly benefit your  
application, we use it for everything in our apps and has greatly  
improved performance, works seamlessly even when replacing or adding  
new parts to the page that have to exhibit similar behavior and  
reduced code clutter. It’s a bit like jQuery’s live function, and it  
supports non bubbling events too, like focus and blur on form elements  
and form submission
- Or you can put the click event observer on the table itself. If you  
add new tables that need highlighting via ajax, you’ll have to attach  
a new observer to that one too, as well as clean up observers before  
destroying elements on the page.

In both cases, inspect the event.target (what element was actually  
clicked), then go .up(‘tr’) and put the highlight class on the row. If  
that needs to be saved in the model, just fire an Ajax.Request from  
your javascript highlight method.

The simple fact of the matter is that you’ll have to write custom  
javascript and not rely on Rails’ built-in helpers to get a performant  
and clean solution.

Best regards

Peter De Berdt
