views:

174

answers:

3

Is there a syntax for the following thing:

$$('.a-lot-of-elems').addEvent('someevent',somefunction);
A: 

something like

var elements = $$('.a-lot-of-elems')
for(var i = 0 ; i < elements.length ; i = i + 1)
{
  elements[i].addEvent(somefunction);
}

should do ya!

thecoshman
$ is used to fetch an element by id. This isn't going to work. You should use $$ to retrieve all elements matching the selector.
Htbaa
@thecoshman thanks, I am familiar with this way, I am looking for a shorter way to write this.
Itay Moav
+3  A: 

First off - the following will work just fine.

$$(selector).addEvents({
    click: fn
});

Don't use for, to iterate through an element collection, use each instead:

$$(selector).each(function(el){
    el.addEvents({
        click: fn
    });
});

Here's a working example: http://jsfiddle.net/EPYBx/

Oskar Krawczyk
+1  A: 

You are just missing the event type.

var someFunction = function(e) {
  alert('clicked!');
}

$$('.a-lot-of-elems').addEvent('click', somefunction);

Alternatively, you can use

$$('.a-lot-of-elems').addEvent('click', function(e) {
  alert('clicked!');
});
Randy Simon
My bad, I meant to give a general example to what I need.
Itay Moav