Your code is correct. Just go through it once again.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetItemID(id) {
alert(id);
var y3 = document.getElementById("htm_anchor" + id);
alert(y3);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlDemo" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
Namespaces
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
for (int i = 0; i < 3; i++)
{
HtmlAnchor htm_anchor = new HtmlAnchor();
htm_anchor.InnerText = "Click here!";
htm_anchor.HRef = "#";
htm_anchor.ID = "htmAnchor" + i.ToString();
htm_anchor.Attributes.Add("runat", "sever");
htm_anchor.Attributes.Add("onclick", "GetItemID('" + i + "')");
pnlDemo.ID = "pnl1";
pnlDemo.Controls.Add(htm_anchor);
}
}
}