I am used to using inline events in my websites for example
<head>
<script>
function func() {
alert("test");
}
</script>
</head>
<body>
<div id="mydiv" onclick="func()"></div>
</body>
I noticed that sites these days do not use inline events at all. I know you can set events pragmatically like:
document.getElementById('mydiv').onclick = function(){ func()}
Is this how it should be done? Do I have to put the above line at the end of every page?
How are the keypress,click, and blur events attached to the input fields on this site for example: https://twitter.com/signup
Thanks.
The cleanest way to add event listeners is to use the built in methods :
This promotes cleaner more readable and reusable code. (Note
attachEventis for pre IE9)You can either place this code at the end of the
<body>tag (or anywhere within the<body>tag but after the DOM element is added) or within a windowonloadfunction – which executes after the DOM has completed loading :