views:

10

answers:

1

I am new to the prototype framework and am trying something really simple and failing. I am trying to respond to a click event on a button like so:

$$('.btn').observe('click', respond);
function respond(event) {
    alert("hello");
}

Why isnt this working?? Please help!

A: 

Unlike jQuery, handing selectors with multiple results in Prototype works a little differently. You need to handle each selected result separately using .each().

$$('.btn').each(function(element) {
    element.observe('click', respond);
})

This is one of the reasons I moved over to jQuery. The other reason: knowing jQuery is marketable and knowing Prototype is not.

Diodeus
Thats just stupid. Thanks. I am from a jquery background and am being forced to use prototype for an upcomming project - from what i have seen it is miles behind jquery with regards to dom manipulation
David
Also - this is not mentioned in the documentation from what I can see!
David
I wouldn't say it's miles behind. It can do all of the same things, but not in such a concise manner as jQuery. I worked with it before jQuery came onto the market. Back then it was by far the best choice. But, yeah, doing things with jQuery is sooo much easier.
Diodeus
see: http://api.prototypejs.org/language/enumerable/
Diodeus
You can also do the same thing by using "invoke".http://api.prototypejs.org/language/enumerable/prototype/invoke/
Diodeus