Why do my event handlers not work in my javascript function.If it's the wrong way to access a button event handler inside a js function than how to do it correctly?

```
<script type="text/javascript">

function cl(){
    var button = document.getElementById('but');
    button.onclick = function (){
        alert('this works');
    }
}

</script>
</head>
<body>
<input type="button" value = "click" id = "but">
</body>

```