This code:
document.getElementById("foo").onfocus = alert("foo");
assigns the result of calling alert () to the onfocus property. What did you mean:
document.getElementById("foo").onfocus = function(){ alert("foo"); };
DOM Level 0, . - , -, - , jQuery:
$('#foo').focus(function(){ alert("foo"); });
, attachEvent IE addEventListener , , , , 0 .