SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Accessing Links in Nested TD Cells with Prototype

       

There must be a better way to do the following with PrototypeJS. I want to loop over nested links inside of table td cells and apply a class to the row that the link is in when the link is clicked. If a user clicks on another link the class will be removed and applied to the current table row.

The code below works but I think it seems hackish. Any thoughts on improving this code?

Here is the link to the pastie http://pastie.org/703316

This is the table with haml markup

%table %tr %td info %td = link_to "be clicked", remote_request_goes_here_path

And the Js


    $$("td.filter_link > a").each(function(element){
        Event.observe(element, "click", function(event){
            $$("td.filter_link").each(function(el){
                if(el != this){
                    el.up().removeClassName("turnwhite");
                }
            });
                       // apply the turnwhite class to the table row
            this.up().up().addClassName("turnwhite");
        });
    });

Tagged w/ #$$ #code optimization #javascript #nested #prototype #tables #tdjavascript

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app