jquery - delete and reset html table row ids for the rest of the table -


how reset html table row id , name after deleting row using jquery. example html table has 5 rows , if delete 3rd row, actual 4th , 5th row textbox id should become 3 , 4

<script type="text/javascript"> $("#add").on("click",function() { var data ='<tr><td>2</td><td><input type="text" id="name2" value=""/></td><td><input type="text" id="phone2" value=""/></td><td><input type="text" id="email2" value=""/></td><td><button id="delete">delete</button></td></tr>'; $("tbody").append(data); }); </script> <button id="add">add row</button> <table> <tbody> <tr> <td>1</td> <td><input type="text" id="name1" value=""/></td> <td><input type="text" id="phone1" value=""/></td> <td><input type="text" id="email1" value=""/></td> <td><button id="delete">delete</button></td> </tr> </tbody> </table> 

so first must declare global counter window.globalcounter = 2; you'll store rows counter. (what index have added row)

then when add increment window.globalcounter++.

and when delete, must fetch currentrow, decrement counter, , refactor indexes.

  $("tbody").on("click", "#delete", function (ev) {          // delete row         var $currenttablerow = $(ev.currenttarget).parents('tr')[0];         $currenttablerow.remove();         window.globalcounter--;          // refactor indexes         $('table').find('tr').each(function (index) {             var firsttddomel = $(this).find('td')[0];             var $firsttdjqobject = $(firsttddomel);             $firsttdjqobject.text(index + 1);         });      }); 

here working code snippet.

window.globalcounter = 2;    $(document).ready(function () {      $("#add").on("click", function () {          var data = '<tr><td>' + window.globalcounter + '</td><td><input type="text" id="name2" value=""/></td><td><input type="text" id="phone2" value=""/></td><td><input type="text" id="email2" value=""/></td><td><button id="delete">delete</button></td></tr>';          $("tbody").append(data);          window.globalcounter++;      });        $("tbody").on("click", "#delete", function (ev) {          var $currenttablerow = $(ev.currenttarget).parents('tr')[0];          $currenttablerow.remove();          window.globalcounter--;                    $('table').find('tr').each(function (index) {              var firsttddomel = $(this).find('td')[0];              var $firsttdjqobject = $(firsttddomel);              $firsttdjqobject.text(index + 1);          });      });      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>  <button id="add">add row</button>  <table>      <tbody>          <tr>              <td>1</td>              <td>                  <input type="text" id="name1" value="" />              </td>              <td>                  <input type="text" id="phone1" value="" />              </td>              <td>                  <input type="text" id="email1" value="" />              </td>              <td>                  <button id="delete">delete</button>              </td>          </tr>      </tbody>  </table>

here have alternative working jsfiddle: https://jsfiddle.net/cn5p4oke/

of course must adjust phone2, email2, name2 ids.

please note used jquery 2.1.0.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -