Search code examples
javascriptprototypejsdom-events

Prototype - click event on an image


I am trying to respond to a click event on a image like so. Why isn't this working?

$$('refresh').each(function(element) {     
    element.observe('click', respond); 
}) 

function respond(event) {     
    alert("hello"); 
} 

<img src="images/refresh.jpg" id="refresh" />

Solution

  • Updated

    See DEMO

    Use $('refresh') instead of $$('refresh'), or $$('#refresh'). But second variant returns array anyway. See links: $ and $$. And I don't understand how do you bind event handler.

    All code:

    <img id="refresh" src="images/refresh.jpg" />
    
    <script>
      $$('#refresh').each(function (element) {     
        Event.observe(element, 'click', respond); 
      });
    
      function respond (event) {
        alert("hello"); 
      }
    </script>