Javascript Form How to - Assign null to onclick and re-add event handler








Question

We would like to know how to assign null to onclick and re-add event handler.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--  www .  java  2  s .  c o m-->
    document.getElementById('clickme').onclick = null;
    document.getElementById('clickme').onclick = function(){ 
       console.log('clicked'); 
    };
}
</script>
</head>
<body>
  <div id="clickme">Click me</div>
</body>
</html>

The code above is rendered as follows: