Add on click event handler for created div element - Javascript DOM

Javascript examples for DOM:Event

Description

Add on click event handler for created div element

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
window.onload=function () {/*  w  ww .j a v  a2s . c  o m*/
   var DivTag = document.createElement("div");
   DivTag.setAttribute('id', 'MyDivTagID');
   DivTag.innerHTML="Click";
   DivTag.onclick = function(e) {
     window.location = "http://www.your server.com/?Url=" + escape(window.location.href);
   }
   document.body.appendChild(DivTag);
 }

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

Related Tutorials