How can I make a for loop in jQuery?
<p>I need to have a <code>for</code> loop inside my jQuery.</p>

<p>Example:</p>

<pre><code>  for(i=0;i&lt;counter;i++)
   {
    $("div"+i+"").click(function(){//some code});
   }
</code></pre>

<p>How can I accomplish this?</p>

<p><hr /></p>

<h3>EDIT:</h3>

<p>This code was posted by the OP in a comment to one of the answers:</p>

<pre><code>$("#displayPanel div").click(function (){ alert($(this).attr("id")); } 

&lt;div id="displayPanel" class="displayPanel"&gt; 
  &lt;div id="heading"&gt; Display Panel &lt;/div&gt; &lt;br/&gt; 
  &lt;div id="save" class="saveClass"&gt;&lt;/div&gt; &lt;br/&gt; 
  &lt;div id="field1" class="my"&gt; 
    &lt;label id="labelstr1"&gt;Untitled1&lt;/label&gt; 
    &lt;input id="inputstr1" type="text"/&gt; 
  &lt;/div&gt; 
  &lt;div id="field2" class="my"&gt; 
    &lt;label id="labelstr2"&gt;Untitled1&lt;/label&gt; 
    &lt;input id="inputstr2" type="text"/&gt; 
  &lt;/div&gt; 
&lt;/div&gt;
</code></pre>

<p>The alert is showing me the id for the first two <code>div</code>s and not for the <code>field1</code> and <code>field2</code>.</p>

<p><hr /></p>

<h3>Note:</h3>

<p>The <code>Field1</code> and <code>Field2</code> <code>div</code>s are created on the fly.</p>
