Re: Applying a function to multiple ids

kangax
Thu, 03 Jul 2008 19:27:29 -0000

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

On Jul 3, 3:13 pm, Amiri
<amiribarksd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> 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