How to create a new user ID for cloned element [duplicate]
-
03-07-2021 - |
Question
Possible Duplicate:
jquery create a unique id
I am cloning a form and want to give a unique ID to each cloned element. How can I do this?
Here is my script:
// Duplicates category select menu
$(".add-color").click(function(){
$(".color-category").clone().removeClass('color-category').appendTo("#we-want-to").find('.submenu-select').addClass('hide');
});
$(".add-color-alternate").click(function(){
$(".color-category-alternate").clone().removeClass('color-category-alternate').appendTo("#we-want-to").find('.submenu-select, .results-table').addClass('hide');
});
HTML:
<div id="we-want-to" class="sortable"></div>
any help would be greatly appreciated.
Solution
Try this...
var i =1;
$(".add-color").click(function(){
$(".color-category").clone().removeClass('color-category').attr('id',i++).appendTo("#we-want-to");
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow