1

Is there a syntax for the following thing:

$$('.a-lot-of-elems').addEvent('someevent',somefunction);
3
  • 5
    That should just work. The element of course should support the event.
    – Htbaa
    Commented Mar 24, 2010 at 15:26
  • ... Although I'm not sure if this works for 1.1.2. What version are you using?
    – Htbaa
    Commented Mar 24, 2010 at 15:34
  • @Htbaa I am using the latest version. Commented Mar 24, 2010 at 17:25

3 Answers 3

4

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/

1

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!');
});
1
  • My bad, I meant to give a general example to what I need. Commented Mar 25, 2010 at 14:21
0

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!

2
  • $ 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
    Commented Mar 24, 2010 at 15:30
  • @thecoshman thanks, I am familiar with this way, I am looking for a shorter way to write this. Commented Mar 24, 2010 at 17:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.