<script type="text/javascript">
window.onload = function(){
for(var i=0; i<3; i++){
var element = document.createElement('div');
element.id = 'id'+i;
element.innerHTML = 'click'+i;
document.body.appendChild(element);
element.onclick = (function(){
return function(){
alert(this.id);
};
})();
}
};
</script>