Query element when DOM content is loaded via event handler for DOMContentLoaded - Javascript DOM

Javascript examples for DOM:Event

Description

Query element when DOM content is loaded via event handler for DOMContentLoaded

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script>
      document.addEventListener("DOMContentLoaded", function(event) {
        document.querySelector('#image1').addEventListener("click",myFunction);
        function myFunction(){/*from  ww  w.ja va  2s  . co  m*/
           console.log("on click");
        }
      });
    
      </script> 
   </head> 
   <body> 
      <img src="http://www.java2s.com/style/download.png" id="image1">  
   </body>
</html>

Related Tutorials