Ah, I see what you’re getting at. I had a similar application lately where I had to clone form fields (and labels) and I’ve gone with a simple regular expression. I’m gonna paste what I’ve used, maybe this will give you a starting point.
Code:
// the variable “item” has been defined earlier in the script and is a wrapper element
// that contained the elements whose values had to be replaced –
// it’s the root of the clone(s)
var ind = item.index();
var clone = item.clone().hide();
clone.find('input[type=text]').val('');
clone.find('input[type=text],label[for]').each(function() {
if($(this).attr('id').length) {
var num = $(this).attr('id');
var att = 'id';
}
else if($(this).attr('for').length) {
var num = $(this).attr('for');
var att = 'for';
}
var newNum = num.replace(/[0-9]/,ind+1);
$(this).attr(att,newNum);
Basically I had IDs and
for attributes in the same style as your classes which I had to number sequentially. Don’t know if replaceAll() is a better way; this has been
my first idea to solve the problem.