Javascript loop

Asked

Viewed 208 times

2

I need when to delete a row from a list. it changes an input items.

Example:

linha 01
linha 02
linha 03

if I delete line two currently it gets:

linha 01
linha 03

but I didn’t want it that way I need it to change and leave like this:

linha 01
linha 02

so I thought I’d do it like this:

for (i = 1; i < quantidadeTR; i++) {
    $("input:text[name^='item']").val(i);           
}

quantityR = is equal to the line total taken by a function.

with the above code I have the result:

linha 02
linha 02

I tried that too but it makes the same mistake:

var count = 1;
    while ( count < quantidadeTR ) {        
        $("input:text[name^='item']").val(count);
    count++;
    };
  • 3

    I don’t know exactly what’s in this table, but if it makes sense to replace it with an ordered list <ol>, the numbering would automatically adjust.

1 answer

6


When you make a query of those in jQuery and calls the method val, you affect all the items that fit the query. That’s why all lines have the same text.

JQuery objects have a function each, iterates over all the items obtained by the query. For it you pass a function that takes two parameters: the first is the index of the current element and the second is the current element itself. What you want is something like:

$("input:text[name^='item']").each(function (index, elem) {
    $(elem).val(index);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.