Event.addBehavior({
'#div1 a:click': function() {
$('body').css({backgroundColor:
'#'+Math.floor(Math.random()*16777215).toString(16)});
}
});
Click me
Event.addBehavior({
'#twitter_trends': function() {
//some code that loads
//current trends from twitter
}
});
var AwesomeBehavior = Behavior.create({
initialize: function() {
this.element //the bound element
},
onclick: function() {
this.element.append($('<span>✔</span>'));
}
});
Event.addBehavior({
'#behaviors p': AwesomeBehavior
})
I have a behavior
var AwesomeBehavior = Behavior.create({
initialize: function() {
this.element //the bound element
this.element.css('color', '#' + randomColor());
},
onclick: function() {
this.element.append($('<span>✔</span>'));
}
});
Event.addBehavior({
'#list li': AwesomeBehavior
})
Note: this only works for behaviors with event methods. That's because it's using jQuery.live to catch any of the bound events on the element and it initializes the behavior when the event is triggered. (the color change on initialize here is meant to demonstrate when the behavior is getting intialized)