How can I have every button on an html page perform the same function using prototype.js? I used the someElement.observe() method and it worked for one button. But how can I do this for every button on the page without coding separately for each button?
+3
A:
Use the css selector to select multiple elements:
$$('input[type="button"]').observe(...)
altCognito
2009-05-28 18:49:52
+2
A:
You can also use event delegation to register one event handler on the element that contains your buttons (e.g. the document body) and take advantage of the fact that events bubble up the DOM.
This will ensure that your event handler is called for every button, even if you dynamically add new buttons to the page.
E.g.
document.observe( 'click', function( event )
{
var elem = event.element();
if ( elem.match( 'input[type="button"]' ) )
{
// Do your event handler
}
});
jimr
2009-06-11 05:17:49