1

I am using below the code to insert a row dynamically in html table.

var newRow = jQuery('<tr><td><input style="width:200px" type="text" name="designation' +
counter + '"/></td><td><input style="width:200px" type="text" id="start_date'+ counter +'" name="start_date' +
        counter + '"/></td><td><input style="width:200px" id="end_date'+ counter +'" type="text" name="end_date' +
        counter + '"/></td></tr>');
    jQuery('table.authors-list').append(newRow);

This code works fine. The row is always inserted as last row. But i need to insert the row just before the last row. I mean if the table has 3 rows, then new row should be in 3rd place the row which was there in 3rd place should be moved to last. How can i achieve this? Please help me.

Thanks!

2 Answers 2

4

You can use the .before() method here like :

jQuery('table.authors-list tr:last').before(newRow);
  • tr:last will get the last row in the authors-list table.
  • .before() will insert the new row before the last one.
Sign up to request clarification or add additional context in comments.

Comments

0
$('table tr:last').before($('<tr>....</tr>'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.