2

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..

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
manas
  • 6,119
  • 10
  • 45
  • 56
  • _"Is it possible using jquery"_ **YES!!** – gdoron Oct 20 '12 at 20:55
  • At the *very* least *show us your HTML*. And, if possible, put together an [SSCCE](http://sscce.org/) demo of what you've got so far at [JS Fiddle](http://jsfiddle.net/) or similar. – David Thomas Oct 20 '12 at 21:04
  • This question is a duplicate: http://stackoverflow.com/questions/171027/add-table-row-in-jquery. – iwein Oct 20 '12 at 21:06

4 Answers4

4

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);
    }
});
kabaros
  • 5,083
  • 2
  • 22
  • 35
3

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>
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
0

It is as simple as: $('#myTable tr:last').after('<tr>...</tr><tr>...</tr>'); with some caveats

I've created a little fiddle for you to experiment with: http://jsfiddle.net/fNW8T/

Personally I like a framework like AngularJS with an ngRepeat, or a just datatable. Doing this by hand is rather clunky.

iwein
  • 25,788
  • 10
  • 70
  • 111