This is very simple javascript function using jQuery library for deleting or adding html table row on user click. Basically, I had to write this functionality for one of my client’s projects. There was a table with some input fields in each row what user can manipulate by inserting data, adding or deleting those rows. So, I made like following picture.
Code is really simple. Consider we have following HTML table with any style sheet.
<table id="options-table"> <tbody> <tr> <td>Input-Box-One</td> <td>Input-Box-Two</td> <td></td> </tr> <tr> <td><input type="text" name="input_box_one[]" /></td> <td><input type="text" name="input_box_one[]" /></td> <td><input class="del" type="button" value="Delete" /></td> </tr> <tr> <td><input type="text" name="input_box_one[]" /></td> <td><input type="text" name="input_box_one[]" /></td> <td><input class="add" type="button" value="Add More" /></td> </tr> </tbody> </table>
If we want more rows then we have to add new row to the table.
$('.add').live('click',function(){ $(this).val('Delete'); $(this).attr('class','del'); var appendTxt = "<tr><td><input type="text" name="input_box_one[]" /></td><td><input type="text" name="input_box_two[]" /></td><td><input class="add" type="button" value="Add More" /></td></tr>"; $("tr:last").after(appendTxt); });
Delete a row from Table.
$('.del').live('click',function(){ $(this).parent().parent().remove(); });
You can even take look at demo or download sourcecode.
alert(“Great Post”);
Very good article!!! Now we can code based on this!!!
Hi!
Thank you! Thank you! Thank you!
The demo link is not working…!
It is a good tutorial/article.
Thank you!
thanks for your comments :). Actually something wrong to demo file. I will fix that soon as possible.
I am a hobistic web designer but still in my early stage of this so i must say Thanks a lot for this very simple and useful article… i was really looking for this…
Really nice tutorial! Thanks a lot for this great post! 🙂
Really nice post! Thanks a lot for this great post!
Thanks for comments 🙂
really nice article, very impressive. thanks for posting.http://www.maritimasaude.com
Thanks .. Keep in touch with my blog ..
Than you soo much for code you give
Thanks for feedback. Keep in touch with my blog for more upcoming posts.
Hi
But it does not work with jquery mobile 1.3.2
Any idea how to migrate to the new version?
Many thanks
Thomas
Thanks for the code, please is it possible to restrict the number of rows?
hi, thanks for sharing. it work. but, let me ask something, how to change button remove into image close, to make attractive ? thanks