0

I am trying to add rows to my repeater using jQuery using below script.It is working perfect in IE, but not in Firefox, Chrome and Safari.

looks like issue with using of outerHTML. Can someone help me?

function AddRowToTable(table) {
    var newRow1 = $(table.rows[table.rows.length - 2].outerHTML);
    var myRow = $(table.rows[table.rows.length - 2]);
    $(myRow).after(newRow1);
}
Chandu
  • 81,493
  • 19
  • 133
  • 134
Bala
  • 105
  • 8
  • 6
    If it only works in IE there's something wrong with it... :) – jcuenod Jul 05 '11 at 18:40
  • You're mixing jQuery and regular Javascript DOM calls. It would probably help you to use one or the other. outerHTML is IE specific. – Cfreak Jul 05 '11 at 18:42

2 Answers2

1

outerHTML is a proprietary standard, unsupported by browsers other than IE. It's a bad idea anyway -- you should almost always just use the DOM nodes and clone them where necessary. Fortunately, jQuery makes this very easy for you.

function AddRowToTable(table) {
    var $table = $(table);
    var $oldRow = $table.find('tr').eq(-2); // get the second last row
    var $newRow = $oldRow.clone(true); // clone the node
    $oldRow.after($newRow); // insert the new row after the old row
}

See:

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • This is absolutely perfect. Thank you very much. Is there any way to read this table data from code behind in c#? I want to maintain this state in postbacks and need to send this to DB after final save. – Bala Jul 05 '11 at 19:35
0

Possible duplicate of Add table row in jQuery

The solution was

$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');

You can probably modify that to being useful in your case...

Community
  • 1
  • 1
jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • Well, the question here is about copying the existing row, so it isn't exactly a duplicate. – lonesomeday Jul 05 '11 at 18:45
  • true, didn't actually realise that was what he was trying to do with his code... Nevertheless, cloning is not the difficulty. – jcuenod Jul 05 '11 at 18:51