Note that there are some explanatory texts on larger screens.

plurals
  1. POauto increment the serial number when row increases and automatically readjust the numbering order when a row gets deleted in jquery
    text
    copied!<p>I want the serial no: to increase whenever a row is inserted dynamically. and when any row is deleted whether in the beginning or in the end or in-between, the numbers should rearrange themselves in proper order. I'm a fresher and could not get a logic on how to do it. Can anyone pls guide me on the same. Thanks in advance for the support. </p> <p>I have given my sample coding below. On clicking the <strong>"(+)"</strong> button, the row increases automatically. when that is happening, i want the serial no also to increment automatically. And when any row is deleted, (even in middle of the table), then the numbers should automatically adjust themselves in proper order. Pls guide me on the same.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready( function() { id=1; var serial =1; $('#butVal').click(function(){ var master = $(this).parents("#table-2"); id++; var sample = master.find('.tabRow').clone(); sample.attr("id", id + "item"); master.find('.tabRow').removeClass('tabRow'); master.find('tbody').append(sample); //alert(master); //alert(sample); //alert(sample.html()); //alert(master.html()); var rowLen = $('#table-2 &gt; tbody &gt;tr').length; //alert(rowLen); if(rowLen&gt;9) { alert("no of row is reached 10"); } } ); $('table#table-2 button.remove').live('click', function(){ if($("table#table-2 tbody tr").length &gt; 1) { if($("table#table-2 tbody tr").index($(this).parents('tr')) ==$("table#table-2 tbody tr").length -1) { $(this).parents('tr').prev().addClass("tabRow"); } $(this).parents('tr').remove(); } else { alert("you can.t remove this record"); } } ); }); //jquery ends here &lt;/script&gt; &lt;style type="text/css"&gt; .total { border:1px solid black; width:90%; height:1250px; margin-left:auto; margin-right:auto; } .add { width:100%; } .add select,input { width:100%; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="total"&gt; &lt;table id="table-2" class="add" border ="1"&gt; &lt;thead&gt; &lt;tr&gt;&lt;th class="small"&gt; S.No&lt;/th&gt;&lt;th&gt; Product Status &lt;/th&gt; &lt;th&gt; Stock Status&lt;/th&gt; &lt;th class="sizing"&gt; Description&lt;/th&gt; &lt;th&gt; Quantity &lt;/th&gt; &lt;th&gt; Price &lt;/th&gt; &lt;th&gt; Total &lt;/th &gt; &lt;th&gt; &lt;button id="butVal"&gt; + &lt;/button&gt;&lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr class="tabRow" id="1item"&gt; &lt;td class="sno"&gt; &lt;/td&gt; &lt;td&gt;&lt;select&gt;&lt;option&gt; New &lt;/option&gt;&lt;option&gt; Old &lt;/option&gt;&lt;/select&gt; &lt;/td&gt; &lt;td&gt;&lt;select&gt;&lt;option&gt; In Stock &lt;/option&gt;&lt;option&gt; Out Of Stock &lt;/option&gt;&lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="desc"/&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="qty"/&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="price"/&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="total"/&gt; &lt;/td&gt; &lt;td&gt;&lt;button class="remove"&gt;Remove&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt; Grand Total &lt;/td&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt; &lt;button id="proceed" onClick="payment();"&gt; Proceed &lt;/button&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;/table&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload