Sankalp Kota
Sankalp Kota

Reputation:

Prototype button onClick

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?

Upvotes: 3

Views: 10277

Answers (3)

hobbit77
hobbit77

Reputation: 11

You can use this also:

Event.observe(window, 'load', function(){
$('idForm').getInputs('radio', 'nameRadio').each(function(el){
el.onclick  = function(){
//Actions<br>

});
});

Upvotes: 1

jimr
jimr

Reputation: 11230

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
  }
});

Upvotes: 3

cgp
cgp

Reputation: 41381

Use the css selector to select multiple elements:

$$('input[type="button"]').observe(...) 

Upvotes: 4

Related Questions