Hola a los dos!!
 
Javier, me han traido montañas de código, y había uno para tí!!
 
Es este:
 
   Código PHP:
    <html>
<head>
    <title>Untitled</title>
    <style>
    .a{background:red;
    width:100px;
    height:100px;}
    </style>
</head>
 
<body>
<div id="pru"></div>
<script>
elem=document.getElementById('pru');
elDiv=document.createElement('div');
elDiv.setAttribute('className','a');
elDiv.setAttribute('id','algo');
elDiv.onclick=pepe
function pepe(esto) {
if (esto){alert(esto.id);}
else{alert(this.id)}}
elem.appendChild(elDiv);
 
</script>
<input value="www" type="button" onclick="pepe(elDiv)"/>
 
</body>
</html>