Write Effective jQuery Code

You have to keep some points in mind while writing jQuery code: 
 
1.Keep DOM insertion as min as possible.For e.g 
Instead of inserting multiple <li> elements in DOM, wrap it in <ul> and then make insertion to DOM. 
 
// wrong approach 
for(var i=0;i<=500;i  ) 
{ 
strHtml ="<li>i</li>"; 
} 
$("ul").html(strHtml); 
 
 // right approach 
strHtml="<ul>"; 
for(var i=0;i<=500;i  ) 
{ 
strHtml ="<li>i</li>"; 
} 
strHtml="</ul>"; 
$("div").html(strHtml); 

Leave a Reply

Your email address will not be published.