title: How do I make my counter work correctly?
body: <p>I have a function that adds div when I press a button.
  I have a counter re-setting logic written in the removal which should re-count all my names.</p>
  
  <p>my problem is that when I add say 3 items, and remove the 2nd Item, my 3rd item does not rename itself.</p>
  
  <p>what am I doing wrong with this?</p>
  
  <pre><code> $(function () {
       var rowItem = $(".row", $(".formitems")); //select all rows from class formitems
       $(".formitems").on("click", ".addRow", function () {
           var newItem = rowItem.clone(),
               rowIndex = $(".row", $(".formitems")).length;
           $(":input", newItem).each(function (c, obj) {
               $(obj).attr("name", $(obj).attr("crap") + rowIndex);
           });
           $(".formitems").append(newItem); // adds At the end of the container
       }).on("click", ".removeRow", function () {
           if ($(".row", $(".formitems")).length &gt; 1) {
               var target = $(this).parent().parent().parent().parent(".row");
               target.slideUp(function () {
                   target.remove();
               });
           }
           for (i = 0; i &lt; $(".row", $(".formitems")).length; i++) //select our div called //formitems and then count rows in it
           {
               rowobj = $(".row", $(".formitems"))[i];
               $(":input", rowobj).each(function (c, obj) {
                   $(obj).attr("name", $(obj).attr("crap") + i);
               })
           }
       });
   });
  </code></pre>
  
  <p>EDIT : Ok, So  I put up alerts just before and after using remove(). Does jquery takes time to re-calulate all the properties? If yes, are they cached? If yes, can I force refresh them.?</p>