Mark Biek
Mark Biek

Reputation: 150759

Binding custom functions to DOM events in prototype?

Jquery has a great language construct that looks like this:

$(document).ready(function() {
    $("a").click(function() {
        alert("Hello world!");
    });
});

As you might guess this, once the document has loaded, binds a custom function to the onClick event of all a tags.

The question is, how can I achieve this same kind of behavior in Prototype?

Upvotes: 3

Views: 8115

Answers (3)

erlando
erlando

Reputation: 6756

Prototype 1.6 provides the dom:loaded event on document:

document.observe("dom:loaded", function() {
    $$('a').each(function(elem) {
        elem.observe("click", function() { alert("Hello World"); });
    });
});

I also use the each iterator on the array returned by $$().

Upvotes: 8

eyelidlessness
eyelidlessness

Reputation: 63519

$(document).observe('dom:loaded', function() {
    $$('a').invoke('observe', 'click', function() {
        alert('Hello world!');
    });
});

Upvotes: 4

David McLaughlin
David McLaughlin

Reputation: 5188

Event.observe(window, 'load', function() { 
     Event.observe(element, 'click', function() { 
         alert("Hello World!");
     });
});

Of course you need to "select" the elements first in Prototype.

Upvotes: 1

Related Questions