Lucky Soni
Lucky Soni

Reputation: 6888

jQuery each increment issue

++++++++++UPDATE++++++++++++++++

On further testing i found that a few times the attr method could not set the attributes. I changes the following line of code to make things simple:

$(this).attr('name', 'yahoo');

after the afterRemoveCurrent event was triggred, i console.log the $(this) on which the above line of code was supposed to execute and to my surprise the attribute was not changed to yahoo. I repeated this several times and observed that a few times the attribute was changed and other times it did not. I used setTimeout to delay the execution of the whole function inside afterRemoveCurrent by 500ms but still same results. This helps?

++++++++++UPDATED CONTENT ENDS ABOVE+++++++++++++++++++++++

I have this little piece of code which is not working as expected. I am using a plugin called sheepit http://www.mdelrosso.com/sheepit/index.php?lng=en_GB (Lets you add multiple form fields):

Live version of the page can be seen here:

The i in this statement $(this).attr('name', 'rooms[' + i + ']' + '.' + arr[1] + '.' + arr[2]); gets evaluated incorrectly sometimes. To be more specific:

The variable i should be same in the following 2 statments (each loop):

$(this).attr('data-index', i); and
$(this).attr('name', 'rooms[' + i + ']' + '.' + arr[1] + '.' + arr[2]);

But somehow a few times when the value of i is 0 in the first statement, it is 1 or some other number in 2nd statement.

var sheepItForm = $('#sheepItForm').sheepIt({
    separator: '',
    allowRemoveLast: false,
    allowRemoveCurrent: true,
    allowAdd: true,
    maxFormsCount: 8,
    minFormsCount: 1,
    iniFormsCount: 0,
    pregeneratedForms: ['pregenerated_form_1'],
    afterRemoveCurrent: function(source) {
        var i = 0;
        $('select.children').each(function() {
            $(this).attr('data-index', i);
            $(this).parent().find('select.child-age-option').each(function() {
                var arr = $(this).attr('name').split('.');
                $(this).attr('name', 'rooms[' + i + ']' + '.' + arr[1] + '.' + arr[2]);
            });
            i++;
        });
    }
});

Upvotes: 0

Views: 728

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337626

Use a closure:

afterRemoveCurrent: function(source) {
    (function(i) {
        $('select.children').each(function() {
            $(this).attr('data-index', i);
            $(this).parent().find('select.child-age-option').each(function() {
                var arr = $(this).attr('name').split('.');
                $(this).attr('name', 'rooms[' + i + ']' + '.' + arr[1] + '.' + arr[2]);
            });
            i++;
        });
    })(0);
}

Upvotes: 1

SarathSprakash
SarathSprakash

Reputation: 4624

Try this

var sheepItForm = $('#sheepItForm').sheepIt({
    separator: '',
    allowRemoveLast: false,
    allowRemoveCurrent: true,
    allowAdd: true,
    maxFormsCount: 8,
    minFormsCount: 1,
    iniFormsCount: 0,
    pregeneratedForms: ['pregenerated_form_1'],
    afterRemoveCurrent: function(source) {
        $('select.children').each(function(i) {
            $(this).attr('data-index', i);
            $(this).parent().find('select.child-age-option').each(function() {
                var arr = $(this).attr('name').split('.');
                $(this).attr('name', 'rooms[' + i + ']' + '.' + arr[1] + '.' + arr[2]);
            });
        });
    }
});

Upvotes: 0

Related Questions