Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new row dynamically in html table using jquery [closed]

Tags:

jquery

enter image description here

In my html table i have several input elements.I want to insert a new row of input elements at the end of the table, when ever the user hits enter button after filling last age column. I hope the picture will make things more clear.. Is it possible using jquery as i am new to jquery. Any kind of help will be appreciated..

like image 312
manas Avatar asked Mar 06 '26 06:03

manas


2 Answers

with some guessing of your table html structure, the general idea is like this:

$('input.last').keyup(function handleEnter(e){
    //enter hit on the input that has class last
    if(e.which == 13) {

       $(this).removeClass('last').unbind('keyup');

       $('.myTable').append('<tr><td><input name="input1"/></td><td><input name="input1"/> </td><td><input name="input1" class="last"/></td></tr>');

       $('.last').keyup(handleEnter);
    }
});
like image 131
kabaros Avatar answered Mar 08 '26 19:03

kabaros


Use your last row's last column as a selector in jquery. Fire it's onclick event and add a new tr in the end of table

code is here

 <script>
            $(document).ready(function () {

                $('#tableId tr:last td:last').click(function () {
                    var tr = $('this').closest('tr');
                    $('#tableId').append(tr);
                });
            });
        </script>
like image 28
Ankush Jain Avatar answered Mar 08 '26 20:03

Ankush Jain