views:

549

answers:

2

There is prototype js function:

Event.observe(element, eventName, handler)

here the element means element's ID.

Is it possible to put here element's class?

I got this element from third party with class attribute only.

A: 

You could use a CSS selector:

Event.observe($$('.someClass'), eventName, handler);
Darin Dimitrov
js error occurred:element.attachEvent is not a function[Break on this error] element.attachEvent("on" + name, wrapper); code is :Event.observe($$('.splitpane-divider'),'mouseover',resizer);
sergionni
+6  A: 

$$ can retrieve elements by css selector, including by class via the period notation .:

$$('.myClass'); // array with all elements that have class "myClass"

To answer your question, Event.observe is the "static" version of observe (for all intents and purposes). As a convenience Prototype automagically makes .observe available off of all DOM elements (fetched with either $ or $$):

Examples:

// get one item by id with $ and attach an event listener:
$('myId').observe(eventName, handler);

// get many items by class with $$ and attach an event listener:
$$('.myClass').each(function(element) {
  element.observe(eventName, handler);
});

// or shorter:
$$('.myClass').invoke('observe', eventName, handler);
Roatin Marth