I struggled with this for quite a while. If you’re looking to do the typical:

var newLink = document.createElement("img");
newLink.setAttribute("onclick", "alert('You clicked me')");

You will be upset to discover this works in Firefox and IE 8, but not IE 7 or IE 6. After some Googling I found a nice explanation by Justin French.

The fix:

var newLink = document.createElement("img");
newLink.onclick = function() { alert("You clicked me"); }

Thankfully this works for Firefox and IE 8-6.