Alex Rivera | Logout

jQuery clone duplicate IDs

Asked 2009-01-06T10:46:32.383
54

I have an HTML element with a large collection of unordered lists contained within it. I need to clone this element to place elsewhere on the page with different styles added (this is simple enough using jQuery).

$("#MainConfig").clone(false).appendTo($("#smallConfig"));

The problem, however, is that all the lists and their associated list items have IDs and clone duplicates them. Is there an easy way to replace all these duplicate IDs using jQuery before appending?

Edit
Report

1 Answer

3

FWIW, I used Dario's function, but needed to catch form labels as well.

Add another if statement like this to do so:

if(element.htmlFor){
var matches = element.htmlFor.match(/(.+)_\d+/);
if(matches && matches.length >= 2)            // Captures start at [1].
  element.htmlFor = matches[1] + "_" + cur_num;
}
answered 2012-02-04T20:38:00.737

Your Answer