It depends on the browsers you have to support, but if you're lucky and only get to worry about modern browsers, you can use .addEventListener()
var thing = document.getElementById('thing');
thing.addEventListener('click', function(e) {
// do something here!
}, false);
If you're not so lucky, then you'll have to come up with a cross browser solution..
var thing = document.getElementById('thing');
addEvent(thing, 'click', function() {
// do something
});
function addEvent(elem, event, callback) {
if (elem.addEventListener) {
elem.addEventListener(event, callback, false);
} else if (elem.attachEvent) { // IE7 and earlier
elem.attachEvent('on' + event, function() {
callback.apply(elem, arguments);
});
} else {
elem['on' + event] = callback;
}
}
Or, if you use a library like jQuery, you can easily normalize the whole process
$('#thing').on('click', function() {
// do something...
});