Listen to the events inside an object - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Listen to the events inside an object

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <div id="button1">
          Button-1 
      </div> 
      <script>
var object1 = {
    button1: document.getElementById('button1'),
    eventHandler: function() {// www  .j a  va 2  s  .  co m
      this.button1.addEventListener('click', this.alertSomething);
    },
    alertSomething: function() {
        console.log('Cool');
    }
};
object1.eventHandler.call(object1);

      </script>  
   </body>
</html>

Related Tutorials