-2

Can anyone help me to convert JavaScript to jQuery? The JavaScript code is:

function addRow()
{       
     var table = document.getElementById("table2");     
     var numOfRows = table.rows.length;     
     var numOfCols = table.rows[numOfRows-1].cells.length;                    
     var newRow = table.insertRow(numOfRows);

     for (var j = 0; j < numOfCols; j++) {                    
          newCell = newRow.insertCell(j);                    
          newCell.innerHTML = "add";
     }
}
Mario S
  • 11,715
  • 24
  • 39
  • 47
apolo90
  • 57
  • 1
  • 9
  • 4
    Why do you want to convert this to jQuery? – Cerbrus Nov 22 '12 at 10:07
  • 2
    duplicate? http://stackoverflow.com/questions/171027/add-table-row-in-jquery – chris Nov 22 '12 at 10:09
  • Because the javascript code should be done using jquery framework... – apolo90 Nov 22 '12 at 10:12
  • @apolo90 the reason you are getting downvotes is because when you ask your exact question it displays in the recommended answers. – chris Nov 22 '12 at 10:13
  • Don't use jQuery for the sake of using jQuery. The library is significantly slower than native javascript, especially on small pieces of code like this. – Cerbrus Nov 22 '12 at 10:14
  • I can kind of see why he would want to do it? jquery is much easier to maintain and is in my opinion worth the slower runtime :) – chris Nov 22 '12 at 10:16
  • duplicate? [Using Append to Copy Table Row](http://stackoverflow.com/questions/3503279/using-append-to-copy-table-row-would-like-to-create-unique-ids) – palaѕн Nov 22 '12 at 10:25

2 Answers2

1

try this

DEMO

 $('#addRow').click(function(){
   var row = $('#table2 tr:eq(0)').clone();
   $(row).find('td').html('add');
   $('#table2').append(row);
});

<table id='table2'>
  <tr><td>test</td></tr>
</table>
​<input type='button' id='addRow' value='click' />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
0

You can try this code that add a new row after the last, I don0t know shich parameters you need

function addRow()
{       
   $('#table2 tr:last').after('<tr><td>some </td></tr><tr><td>text</td></tr>');
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171