I'm using the code below within a loop. It works so long as I'm not using IE.
var remove = document.createElement("input");
remove.type = "button";
remove.value = "x";
if (remove.addEventListener) {
remove.addEventListener("click", (function(item_id) { return function() { remove_from_cart(item_id); } })(item_id), false);
} else {
remove.attachEvent("click", (function(item_id) { return function() { remove_from_cart(item_id); } })(item_id));
}
IE needs to have the on when describing the event so this is what you need.
remove.attachEvent("onclick", (function(item_id) { return function() { remove_from_cart(item_id); } })(item_id));
}