Girijasankar Dash
Girijasankar Dash

Reputation: 11

How to create a new id of a dom in jQuery when cloning that dom element

Actually I want to create an unique id for every DOM element which is cloning when the DOM element is dropped in a container and show it in a text area. That text area will show every DOM element's unique id in a list which is dropped in the container. Answer should be in jQuery.

Can any one there to help me?

Upvotes: 0

Views: 445

Answers (2)

Dennis George
Dennis George

Reputation: 1515

for(var i=0; i<=10;i++)
  $('#clonedSource').clone().attr('id', 'newElementId'+i).appendTo('#container');

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074138

Just spin through the elements (using jQuery, or just a boring old recursive descent function using the DOM) and set their id property to something unique.

To set the id of an element using the DOM:

element_reference.id = newId;

With jQuery (1.6 and higher):

jquery_instance.prop("id", newId);

Upvotes: 1

Related Questions