I have a large number of buttons in my page and i want to add a click event to each of the buttons , that is when the user click the buttons it shows the corresponding button value.
i have already implement a method with the help of anonymous functions. i have included my code below
HTML
<div id="div1"/>
Javascript
$(document).ready(function(){
for(var i =0;i<100;i++)
{
$("#div1").append("<input type='button' class='myclass' id='mybutton" + i + "' value='mybutton" + i + "'> ");
};
$(".myclass").click(function(e){
alert(e.target.id);
});
});
It is working perfectly, the problem is an anonymous function is created for each button on this Form, that is if i have 10k elements in my page then anonymous function will created 10k times, i think i causes big memory wastage
. Is there any better way for achieving this ? please help me to solve this , any help will be greatly appreciated . Thanks