Forum: Rails Spinoffs (closed, excessive spam) Applying a function to multiple ids

Posted by Amiri (Guest)
on 2008-07-03 21:14
(Received via mailing list)
Hey folks.

I am trying to apply a function to multiple tables all with different
IDs. I got about halfway through copying and pasting the function and
just changing the ID before I realized this was absurd. But I don't
know how to do this smarter with Prototype. Here is the function:

Event.addBehavior({
  'table#BULLETINS': function() {
      var links = $$('table#BULLETINS
tr.header').first().getElementsByTagName('a');
      for (i=0; i<links.length; i++){
          var link = links[i];
          link.onclick = function() {return false;}
      }
  }
});

So, I was copying this same code, changing "BULLETINS" to whatever ID
I required. How do you parametrize this exactly? I have an arbitrary
number of different IDs to apply this to, but I don't know how to get
Prototype to watch for *any* of them, and then use the found ID,
whichever it is, in this function.

Amiri
Posted by kangax (Guest)
on 2008-07-03 21:29
(Received via mailing list)
1) Simply get all tables (if those that you need are the only ones on
the page):
    $$('table');

2) Assign a unique class to all of the tables, then do:
    $$('table.myUniqueClassName');

3) Specify id's directly in selector expression (if the amount of
tables is not too high):
    $$('table#foo, table#bar, table#baz');

4) Get all tables with id attribute:
    $$('table[id]');

etc.

-- kangax
Posted by Amiri (Guest)
on 2008-07-03 23:51
(Received via mailing list)
On Jul 3, 3:27 pm, kangax <kan...@gmail.com> wrote:
>
> 4) Get all tables with id attribute:
>     $$('table[id]');
>


I think 3 is what I need. That will most directly enable me to control
explicitly which tables the function gets applied to. I don't like the
"grab em all and winnow them" approaches. Thank you, kangax!

Amiri
This topic is locked and can not be replied to.