I have been writing a javascript code that creates a button. But I'm unable to attach event handler to buttons. When I try to get child, it returns undefined. There are two functions, one creates button when window loads and other function tries to get the children.
window.onload = function ()
{
f1();
reader();
}
function f1()
{
var x = document.getElementById("buttons");
for(var i=9;i>=0;i--)
{
var y = document.createElement("BUTTON");
y.setAttribute("class","btn btn-primary btn-lg");
y.setAttribute("id","mybtn");
var z = document.createTextNode(i);
y.appendChild(z);
x.appendChild(y);
}
function reader()
{
var x = document.getElementById("mybtn").firstChild;
alert(x);
}
<div id="stage">
<div id="screen">
</div>
<div id="buttons">
</div>
<div id="sb">
</div>
</div>