title: How to add click event to dynamic id
body: <p>I have a <code>ul</code> list which will be created dynamically with increasing number. I have a <code>for</code> loop to bind the click even to all those <code>UL</code>. </p>
  
  <p>What i need to do is when user click on it, remove the previously selected list and add the clicked as selected.</p>
  
  <p>Somehow the <code>k</code> value in the for loop is not correct for the selection of the increasing list, But it is correct for the click function. </p>
  
  <p>I want to know why it(<code>k</code>) is not correct? As it is correctly binded for the click event for all 5 <code>ul</code> but not inside the click event.</p>
  
  <p>When i console.log the <code>k</code> value it prints <code>6</code> for all ul.  I want to know the reason. How to solve this? </p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>jQuery(document).ready(function(){
      for(var k=1;k&lt;=5;k++){
          jQuery("#sample"+k+" li").click(function(){
              console.log(k);
              jQuery("#sample"+k+" li.selected").removeAttr('class');//removing previous selection
              jQuery(this).addClass('selected');//adding selection for clicked one
          });
      }
  });
  </code></pre>
  
  <p><a href="http://jsfiddle.net/x2qzoeLf/2/" rel="nofollow">JSFIDDLE</a></p>