<input id="myElement" type="button" value="Click me!" />
<script type="text/javascript">
document.getElementById('myElement').onclick = function () {
alert('Hello, world!');
}
</script>
Make sure that you either run this after the element already exists (the scripts below), or when the DOM is ready. (You can use it window.onloadfor this, but you can just use jQuery from the very beginning to, by the way, get the DOM-ready magic function. It onloadhas some drawbacks, such as waiting for images to load.)
source
share