I have a list with 200 divs,
<div id="sId_1" class="sDiv"></div>
<div id="sId_2" class="sDiv"></div>
....
<div id="sId_200" class="sDiv"></div>.
What of the following two is faster?
-- each loop -- with i++;
<div onclick="callSomeFunction('click');" id="sId_'+i+'" ></div>
-- each loop --
-- each loop -- with i++;
$('#sId_'+i+'').click( function (event) ....
-- each loop --
$('.sDiv').click( function (event) ......
Is it the first solution? Because jQuery creates an object in the $.cache for every div.
PS: is an object with more than 1000 entries a problem for JavaScript?
You can use event delegation to attach a single “smart” event handler to a suitable parent container:
Advantages of the above approach:
makes
.delegatecalls chainable.documentelement as is the case with.live.Reference:
http://api.jquery.com/delegate/