Onclick prototype button

How can I get every button on an html page to perform the same function using prototype.js? I used the someElement.observe () method and it worked for a single button. But how can I do this for each button on a page without coding separately for each button?

+3
source share
3 answers

Use the css selector to select several items:

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

You can also use event delegation to register one event handler on an element that contains your buttons (for example, the body of the document), and take advantage of the fact that events cause a bubble in the DOM.

, , .

.

document.observe( 'click', function( event )
{
  var elem = event.element();
  if ( elem.match( 'input[type="button"]' ) )
  {
    // Do your event handler
  }
});
+3

You can also use this:

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

});
});
+1
source

Source: https://habr.com/ru/post/1709322/


All Articles