3

I have a table on which I have a hidden line that I clone in order to add new lines dynamically.

var $clone = $('#table-invoicing').find('tr.hide').clone(true).removeClass('hide');
$('#table-invoicing').find('table').append($clone);

Each line have a id and a data-type. The hidden line is set an id ending in 99. I would like to change this id when I clone the hidden line.

I found similar topics, but for some reason I don't manage to include it in my script. When I clone the line, then there is 2 elements with same id, so a selector by id won't work.

I tried :

$clone.$('#invoicing-row-descr-99').attr("id", "newID");

but then it tells me that $clone is not a function.

Any idea ?

Vincent Teyssier
  • 2,146
  • 5
  • 25
  • 62

2 Answers2

4
$clone.$('#invoicing-row-descr-99').attr("id", "newID");

but then it tells me that $clone is not a function.

Because $clone is an object. Just use attr or prop for the cloned element:

$clone.attr("id", "newID");//change cloned element id

As per your comment, use like this:

$clone.find('your_element').attr("id", "newID");
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
3

.prop() Is a good practice in current versions of jQuery.

$clone.prop("id", "yourId");

You'll need to use it before you are appending it.

rrk
  • 15,677
  • 4
  • 29
  • 45