Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery append and remove dynamic table row

Tags:

I can add many rows for a table, but I can't remove many rows. I only can remove 1 row per sequential add.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function(){     $("#addCF").click(function(){         $("#customFields").append('<tr valign="top"><th scope="row"><label for="customFieldName">Custom Field</label></th><td><input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> &nbsp; <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> &nbsp; <a href="javascript:void(0);" id="remCF">Remove</a></td></tr>');         $("#remCF").on('click',function(){             $(this).parent().parent().remove();         });     }); }); </script>  <table class="form-table" id="customFields"> <tr valign="top">     <th scope="row"><label for="customFieldName">Custom Field</label></th>     <td>         <input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> &nbsp;         <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> &nbsp;         <a href="javascript:void(0);" id="addCF">Add</a>     </td> </tr> </table> 

You can see the code at http://jsfiddle.net/3AJcj/

I need help.

like image 289
user770668 Avatar asked Apr 24 '13 04:04

user770668


People also ask

How add and remove dynamic rows in jQuery?

remove() method we can dynamic add and delete row using jquery. append() method is used for append or add rows inside an HTML table and . remove() method to remove or delete table rows as well as all data inside it from the DOM dynamically with jquery.

How do you Dynamic create table row on each button click in jQuery?

Adding a row: Then we will use the jQuery “click” event to detect a click on the add row button and then use the . append() method of jQuery to add a row in the table. Each row element has been assigned an id Ri that we will later use to delete a row. Each element has a row index column and remove the button column.

How can we count dynamically added table rows in jQuery?

Answer: Use the length Property You can simply use the length property to count or find the number of rows in an HTML table using jQuery. This property can be used to get the number of elements in any jQuery object.


2 Answers

You only can have one unique ID per page. Change those IDs to classes, and change the jQuery selectors as well.

Also, move the .on() outside of the .click() function, as you only need to set it once.

http://jsfiddle.net/samliew/3AJcj/2/

$(document).ready(function(){     $(".addCF").click(function(){         $("#customFields").append('<tr valign="top"><th scope="row"><label for="customFieldName">Custom Field</label></th><td><input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> &nbsp; <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> &nbsp; <a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');     });     $("#customFields").on('click','.remCF',function(){         $(this).parent().parent().remove();     }); }); 
like image 121
Samuel Liew Avatar answered Oct 07 '22 04:10

Samuel Liew


You can dynamically add and delete table rows like this in the image using jQuery.. enter image description here

Here is html part...

<form id='students' method='post' name='students' action='index.php'>      <table border="1" cellspacing="0">       <tr>         <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>         <th>S. No</th>         <th>First Name</th>         <th>Last Name</th>         <th>Tamil</th>         <th>English</th>         <th>Computer</th>         <th>Total</th>       </tr>       <tr>         <td><input type='checkbox' class='case'/></td>         <td>1.</td>         <td><input type='text' id='first_name' name='first_name[]'/></td>         <td><input type='text' id='last_name' name='last_name[]'/></td>         <td><input type='text' id='tamil' name='tamil[]'/></td>         <td><input type='text' id='english' name='english[]'/> </td>         <td><input type='text' id='computer' name='computer[]'/></td>         <td><input type='text' id='total' name='total[]'/> </td>       </tr>     </table>      <button type="button" class='delete'>- Delete</button>     <button type="button" class='addmore'>+ Add More</button>     <p>     <input type='submit' name='submit' value='submit' class='but'/></p> </form> 

Next need to include jquery...

<script src='jquery-1.9.1.min.js'></script> 

Finally script which adds the table rows...

<script>   var i=2;   $(".addmore").on('click',function(){     var data="<tr><td><input type='checkbox' class='case'/></td><td>"+i+".</td>";         data +="<td><input type='text' id='first_name"+i+"' name='first_name[]'/></td> <td><input type='text' id='last_name"+i+"' name='last_name[]'/></td><td><input type='text' id='tamil"+i+"' name='tamil[]'/></td><td><input type='text' id='english"+i+"' name='english[]'/></td><td><input type='text' id='computer"+i+"' name='computer[]'/></td><td><input type='text' id='total"+i+"' name='total[]'/></td></tr>";         $('table').append(data);         i++; }); </script> 

Also refer demo & tutorial for this dynamically add & remove table rows

like image 20
muni Avatar answered Oct 07 '22 05:10

muni