frosty
frosty

Reputation: 5370

jquery selecting and copying tr

I wish to duplicate a tr tag in jquery. I have the following. The issue with this code is that $(rowId) doesn't include the "tr" but just the contents inside it. So the first tag is a <td> not a <tr>. How can select this element also.

      $('.addLine').click(function () {
          var rowId = $(this).attr('rel');
          var rowId = "#" + rowId;
          var newRow = $(rowId);
          var htmlStr = $(newRow).html(); 
          $(newRow).append(htmlStr);

      });

Upvotes: 0

Views: 98

Answers (2)

programmer
programmer

Reputation: 318

Try this one

$('.addLine').click(function () {
          var rowId = $(this).attr('rel');
          var rowId = "#" + rowId;
          var newRow = $(rowId).parent();
          var htmlStr = $(newRow).html(); 
          $(newRow).append(htmlStr);

});

Upvotes: 0

Bj&#246;rn
Bj&#246;rn

Reputation: 2648

.html() returns the inside of the element selected, you can use .clone() to make a copy of the entire element, which you can append.

 $('.addLine').click(function () {
      var rowId = $(this).attr('rel');
      var newRow = $("#"+rowId).clone().appendTo("youTableOrSomething");
  });

Upvotes: 4

Related Questions