Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery clone elements without id

Tags:

jquery

clone

How can i change the id while cloning?

Example:

<div id="unique" class="cloneMe">test</div>
<div id="unique1" class="cloneMe">test</div>
<div id="unique2" class="cloneMe">test</div>

Script

jQuery('.cloneMe').clone(); // has all selected after cloning
  ...

Result

<div id="unique" class="cloneMe">test</div>
<div id="unique1" class="cloneMe">test</div>
<div id="unique2" class="cloneMe">test</div>
<div id="unique" class="cloneMe">test</div>
<div id="unique1" class="cloneMe">test</div>
<div id="unique2" class="cloneMe">test</div>

How can i explicitly modify only the cloned div?

like image 462
Grim Avatar asked Feb 11 '23 07:02

Grim


1 Answers

You can use each and then set a new id.

jQuery('.cloneMe').clone().each(function(i){
  this.id = "id"+ i; // to keep it unique
}).appendTo("someSelector");
like image 184
Amit Joki Avatar answered Feb 13 '23 21:02

Amit Joki