title: Add jQuery click handler to multiple elements?
body: <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++)
     {
      $(&quot;div&quot;+i+&quot;&quot;).click(function(){//some code});
     }
  </code></pre>
  <p>How can I accomplish this?</p>
  <hr />
  <h3>EDIT:</h3>
  <p>This code was posted by the OP in a comment to one of the answers:</p>
  <pre><code>$(&quot;#displayPanel div&quot;).click(function (){ alert($(this).attr(&quot;id&quot;)); } 
  
  &lt;div id=&quot;displayPanel&quot; class=&quot;displayPanel&quot;&gt; 
    &lt;div id=&quot;heading&quot;&gt; Display Panel &lt;/div&gt; &lt;br/&gt; 
    &lt;div id=&quot;save&quot; class=&quot;saveClass&quot;&gt;&lt;/div&gt; &lt;br/&gt; 
    &lt;div id=&quot;field1&quot; class=&quot;my&quot;&gt; 
      &lt;label id=&quot;labelstr1&quot;&gt;Untitled1&lt;/label&gt; 
      &lt;input id=&quot;inputstr1&quot; type=&quot;text&quot;/&gt; 
    &lt;/div&gt; 
    &lt;div id=&quot;field2&quot; class=&quot;my&quot;&gt; 
      &lt;label id=&quot;labelstr2&quot;&gt;Untitled1&lt;/label&gt; 
      &lt;input id=&quot;inputstr2&quot; type=&quot;text&quot;/&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>
  <hr />
  <h3>Note:</h3>
  <p>The <code>Field1</code> and <code>Field2</code> <code>div</code>s are created on the fly.</p>