Adding a new span element to the label and writing an increment in the span for each new fieldset

Viewed 23

I have a plugin for cloning fieldsets in form. For each new fieldset, the plugin generates a new fieldset element with the same classes and subelements. In the beginning, I have only one fieldset and probably that is my problem.

I want to have a new span element in ".myClass" and in this span number, for every new +1. I have: Label Label Label

and I want to have: Label 1 Label 2 Label 3 ...

I have found jQuery code and ist working, but for each new fieldset set number 1:

$(".myClass").each(function(i) {
    $(this).append($("<span>"+(i+1)+"</span>"));
});

I think the problem is because I have only one fieldset in the beginning. When I click and clone a new filedset, jQuery sees the same element.

Any solution/hint/help?

1 Answers

Can you please share your code? I don't get any idea. As per my understand If you find the length of total span inside your myclass then Your issue will resolved

Like $(".myClass > span").length

Or Inside foreach $(".myClass").each(function() { $(this).children('span').length });

Related