Fetching an ID on onclick of a button
<p>I am trying to create button dynamically inside a loop. onclick of button i want to fetch the id of the button. The below code is not working. Can anyone please help me.    </p>

<pre><code> for(int i=1;i&lt;5;i++)
    {
    var btn = document.createElement("button");
    btn.innerText = "button"+i;
    btn.setAttribute("id", i);
    btn.onclick = function(this){
    alert(this.id);
    }
    }
</code></pre>

<p>Thanks in advance</p>