<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function setElement(element) {
return document.createElement(element);
}
function getElement(element) {
return document.getElementById(element);
}
function append(element1,bool,element2) {
bool=bool || false;
if(!bool) {
return document.body.appendChild(element1);
} else {
return element1.appendChild(element2);
}
}
var div=setElement('div');
div.id='div';
append(div);
var button=setElement('button');
button.innerHTML='Click';
append(div,true,button);
var i=0;
button.onclick=function() {
var span=setElement('span');
span.id='span'+i;
span.innerHTML='Text span'+i;
span.onclick=function() {
alert(this.id);
};
append(div,true,span);
++i;
}
</script>
</body>
</html>